StrSplit not splitting? Topic is solved
StrSplit not splitting?
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?
Last edited by PepeLapiu on 01 Jan 2022, 14:05, edited 1 time in total.
- flyingDman
- Posts: 2840
- Joined: 29 Sep 2013, 19:01
Re: StrSplit not splitting?
Pictures don't say anything. They are too small. Post the script.
14.3 & 1.3.7
Re: StrSplit not splitting?
Look again. I accidentally posted the thumbnails instead of full pics.flyingDman wrote: ↑01 Jan 2022, 14:03Pictures don't say anything. They are too small. Post the script.
I can't post the script itself as I am on my Android.
Re: StrSplit not splitting?
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?
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
Re: StrSplit not splitting?
Code: Select all
array := StrSplit("word1 word2", A_Space)
MsgBox % "01:`t" . array[1] . "`n02:`t" . array[2]
Re: StrSplit not splitting?
@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?
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?
`n is a character that belongs between quote marks in expression syntax.
Re: StrSplit not splitting?
So how much of an illegal character was it? Should I lawyer up and invoke the 5th?gregster wrote: ↑01 Jan 2022, 15:33`n is a character that belongs between quote marks in expression syntax.
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?
- flyingDman
- Posts: 2840
- Joined: 29 Sep 2013, 19:01
Re: StrSplit not splitting?
lol. Your now sentenced to read this: https://www.autohotkey.com/docs/misc/EscapeChar.htm
14.3 & 1.3.7
Re: StrSplit not splitting?
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.Xtra wrote: ↑01 Jan 2022, 14:16Next time post your script not an image of it.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
Sup with that?
Re: StrSplit not splitting?
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?
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:
Also read about expression syntax and study the section of the tutorial on when to use percents until you thoroughly understand it.
Code: Select all
MyVar := "hello"
hello := 3
MsgBox, % "Result of the double-deref: " %MyVar% ; output is 3, not hello
Re: StrSplit not splitting?
Adding to what Boiler said: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?
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"])
to:
Array := StrSplit(Clipboard ,[A_Space,"`n"])
HTH
Re: StrSplit not splitting?
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.
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.