Page 1 of 5

StrSplit not splitting?

Posted: 01 Jan 2022, 14:00
by PepeLapiu
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

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 14:03
by flyingDman
Pictures don't say anything. They are too small. Post the script.

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 14:08
by PepeLapiu
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.

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 14:15
by boiler
Those are separated by newline characters `n or `r or both, so you have to split or parse them based on those characters.

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 14:16
by Xtra

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.

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 14:20
by BoBo

Code: Select all

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

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 14:24
by boiler
@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.

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 14:28
by PepeLapiu
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:

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 14:30
by BoBo
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:

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 15:14
by PepeLapiu
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?

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 15:33
by gregster
`n is a character that belongs between quote marks in expression syntax.

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 15:53
by PepeLapiu
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?

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 16:02
by flyingDman
lol. Your now sentenced to read this: https://www.autohotkey.com/docs/misc/EscapeChar.htm

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 16:05
by gregster
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... :)

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 16:12
by boiler
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`.

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 20:15
by PepeLapiu
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

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 20:18
by PepeLapiu
Here is the offending line of code the screenshot above is referring to:

Code: Select all

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

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 20:46
by boiler
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.

Re: StrSplit not splitting?

Posted: 01 Jan 2022, 21:50
by Xtra
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

Re: StrSplit not splitting?

Posted: 02 Jan 2022, 10:29
by PepeLapiu
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.