StrSplit not splitting? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

StrSplit not splitting?

Post by PepeLapiu » 01 Jan 2022, 14:00

Pictures say it all. At the bottom of the script, I included the content of the clipboard into a comment. How do I separate array[1], [8], and [10] into two separate elements?

Image

Image

Image
Last edited by PepeLapiu on 01 Jan 2022, 14:05, edited 1 time in total.

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: StrSplit not splitting?

Post by flyingDman » 01 Jan 2022, 14:03

Pictures don't say anything. They are too small. Post the script.
14.3 & 1.3.7

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 01 Jan 2022, 14:08

flyingDman wrote:
01 Jan 2022, 14:03
Pictures don't say anything. They are too small. Post the script.
Look again. I accidentally posted the thumbnails instead of full pics.
I can't post the script itself as I am on my Android.

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: StrSplit not splitting?

Post by boiler » 01 Jan 2022, 14:15

Those are separated by newline characters `n or `r or both, so you have to split or parse them based on those characters.

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: StrSplit not splitting?

Post by Xtra » 01 Jan 2022, 14:16

Code: Select all

haystack := " 
(
pending
Contract to sell 0.00123459 BTC for 100 CAD

Your counterparty: PepeProton

Link to the offer
)"

array := StrSplit(haystack, [A_Space,"`n"])

for key, val in array
    MsgBox,, haystack, % "index: " . key . "`nvalue = " . val
Next time post your script not an image of it.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: StrSplit not splitting?

Post by BoBo » 01 Jan 2022, 14:20

Code: Select all

array := StrSplit("word1 word2", A_Space)
MsgBox % "01:`t" . array[1] . "`n02:`t" . array[2]
HTH

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: StrSplit not splitting?

Post by boiler » 01 Jan 2022, 14:24

@BoBo - It was already splitting based on spaces. The issue is that it wasn’t splitting when the only separators are newline characters as Xtra’s code addresses.

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 01 Jan 2022, 14:28

Xtra wrote:
01 Jan 2022, 14:16
Next time post your script not an image of it.
The problem is, posting just the script would not have shown the resulting MsgBox.
But next time, I'll just c&p the script into an email to my phone, I guess.
Anyways, thank you all for your help.

HAPPY NEW YEAR!!! :beer:

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: StrSplit not splitting?

Post by BoBo » 01 Jan 2022, 14:30

boiler wrote:
01 Jan 2022, 14:24
@BoBo - It was already splitting based on spaces. The issue is that it wasn’t splitting when the only separators are newline characters as Xtra’s code addresses.
Got it. Thx for the clarification :thumbup:

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 01 Jan 2022, 15:14

boiler wrote:
01 Jan 2022, 14:15
Those are separated by newline characters `n or `r or both, so you have to split or parse them based on those characters.
Which brings me to my next question. As you can see in the script, I used n` which didn't actually do anything. The reason why I did that is because when I tried `n I got an error, claiming I am using illegal characters.
What gives?

gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: StrSplit not splitting?

Post by gregster » 01 Jan 2022, 15:33

`n is a character that belongs between quote marks in expression syntax.

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 01 Jan 2022, 15:53

gregster wrote:
01 Jan 2022, 15:33
`n is a character that belongs between quote marks in expression syntax.
So how much of an illegal character was it? Should I lawyer up and invoke the 5th?
I just laughed when I read it. I never though I could have been such a bad boy in learning AHK.
What is the worst getting caught with? Illegal drugs or illegal characters?

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: StrSplit not splitting?

Post by flyingDman » 01 Jan 2022, 16:02

lol. Your now sentenced to read this: https://www.autohotkey.com/docs/misc/EscapeChar.htm
14.3 & 1.3.7

gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: StrSplit not splitting?

Post by gregster » 01 Jan 2022, 16:05

PepeLapiu wrote:
01 Jan 2022, 15:53
What is the worst getting caught with? Illegal drugs or illegal characters?
Depends on your local jurisdiction. On my local keyboard layout, ` is even a dead key. Criminal investigations are still going on... :)

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: StrSplit not splitting?

Post by boiler » 01 Jan 2022, 16:12

PepeLapiu wrote: What is the worst getting caught with? Illegal drugs or illegal characters?
I assumed illegal drugs led to the the use of n`.

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 01 Jan 2022, 20:15

Xtra wrote:
01 Jan 2022, 14:16

Code: Select all

haystack := " 
(
pending
Contract to sell 0.00123459 BTC for 100 CAD

Your counterparty: PepeProton

Link to the offer
)"

array := StrSplit(haystack, [A_Space,"`n"])

for key, val in array
    MsgBox,, haystack, % "index: " . key . "`nvalue = " . val
Next time post your script not an image of it.
Well, I tried it and it spit/out an illegal character error at me. I even c&p your code. And same result. I had to remove the "`n" part to stop getting an error code.
Sup with that?
Image

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 01 Jan 2022, 20:18

Here is the offending line of code the screenshot above is referring to:

Code: Select all

Array := StrSplit(%Clipboard% ,[A_Space,"`n"])

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: StrSplit not splitting?

Post by boiler » 01 Jan 2022, 20:46

Look at Xtra’s code. There are no % symbols around haystack, so why would you put them around Clipboard? Read the error message carefully. It’s showing that you tried to use a variable name that is actually the contents of the clipboard. That’s what putting % symbols around a variable in an expression means. It says to use the variable name that is contained in the specified variable. See dynamic variables. Here’s a demonstration of a dynamic variable, which you inadvertently created:

Code: Select all

MyVar := "hello"
hello := 3
MsgBox, % "Result of the double-deref: " %MyVar% ; output is 3, not hello
Also read about expression syntax and study the section of the tutorial on when to use percents until you thoroughly understand it.

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: StrSplit not splitting?

Post by Xtra » 01 Jan 2022, 21:50

PepeLapiu wrote: Well, I tried it and it spit/out an illegal character error at me. I even c&p your code. And same result. I had to remove the "`n" part to stop getting an error code.
Sup with that?
Adding to what Boiler said:
Removing `n in what i posted didn't do anything. Here is what happened in your code: You copied some text with spaces in it. Then by using %ClipBoard% in the StrSplit() function you deref the clipboard variable and now the variable name is...the clipboard contents. You cannot have a variable name with spaces in it. (illegal char in your error message) Read what Boiler linked about Expression syntax.

Run what i posted by itself and you will see how it should work.

Then in your code change:
Array := StrSplit(%Clipboard% ,[A_Space,"`n"]) :thumbdown:
to:
Array := StrSplit(Clipboard ,[A_Space,"`n"]) :thumbup:

HTH

PepeLapiu
Posts: 324
Joined: 19 Jun 2020, 14:06

Re: StrSplit not splitting?

Post by PepeLapiu » 02 Jan 2022, 10:29

Yeah. Syntax and variable handling are two of my most common problems. For example I just spent 2 days trying to make StrSplit work for me when nothing was working. I tried adding and removing % signs, dotting I's and crossing T's for 2 days to no avail. Until I figured out it should be StrSplit() and not StrSplit_() with a space.

And the whole time I thought it was the way I was handling my variables that wasn't working. So I did watch a ton of tuts and read lots. Some of it outdated, some of it conflicting. And at times it just confused me more than anything else when you try fix what"s not broke while completely ignoring what is broken.

Post Reply

Return to “Ask for Help (v1)”