Page 1 of 1
String parsing
Posted: Tue Apr 21, 2015 9:14 am
by drhoward
In my program I'm reading a text file and then parsing it using itemDelimiter. The parsing seems to work if I read a tab delimited file and "set the itemDelimiter to tab". I can then access word 1, word 2 etc. and any leading spaces are ignored.
However, if I use a comma delimited text file, and "set the itemDelimiter to comma", the program behaves as if I had "set the itemDelimiter to space". In other words, it picks up the words with the comma included in them.
I'm using LiveCode version 7.0.3 so please advise.
Thank you!
Re: String parsing
Posted: Tue Apr 21, 2015 9:18 am
by Thierry
Hi,
You might check for item keyword in the dictionary, but not word.
HTH,
Thierry
Re: String parsing
Posted: Tue Apr 21, 2015 12:47 pm
by FredBeck
Hi,
Dictionary says it "probably isn't suitable for use in a general-purpose language parser", but the token keyword has been very useful to me.
F.
Re: String parsing
Posted: Tue Apr 21, 2015 2:43 pm
by dunbarx
Hi.
I am not sure what you mean. If you have "abc ,def xyz, qwe", and you ask for:
item 1, you get "abc " --trailing spaces
item 2, you get "def xyz" --interior spaces
item 3: " qwe" --leading spaces
Spaces are just characters. They come over just like any other, delimited by commas, or whatever. What are you seeing?
Craig Newman
Re: String parsing
Posted: Tue Apr 21, 2015 3:56 pm
by drhoward
Part of it started working when I completely exited LiveCode and then used the program again later. This has happened once before while debugging another section of code. I guess that sometimes a stack has to be closed and reopened before proceeding with the debugging process. Usually using the apply button and then saving seems to be sufficient.
However, the following is still not working:
For testing, I used notepad to create a one line text file with a one line, 3 number string, separated by commas. I then had my program read the text file. The following string: 25,34,0.23 does not parse into 3 words with itemDelimiter set to comma. I created a second file with a one line, 3 number, string separated by commas, with a leading blank before the second and third number. When the file was read in by my program, this string did parse into three distinct words: 25, 34, 0.23
I used the code sequence ---- answer "itemDelimiter = " & itemDelimiter" ---- to confirm that itemDelimiter was set to be a comma.
I expect that to get my program working, I'll have to have my program read the file and scan my file buffer, inserting a space character after each comma. Hopefully, it will then parse properly into words using itemDelimiter set to be a comma.
Re: String parsing
Posted: Tue Apr 21, 2015 4:15 pm
by FourthWorld
drhoward wrote:The following string: 25,34,0.23 does not parse into 3 words with itemDelimiter set to comma.
Thierry's post above is well worth heeding: words are not necessarily items.
If you're parsing by items use the item chunk type, not the word chunk type, e.g.:
...should put "34" into the Message Box.
Re: String parsing
Posted: Tue Apr 21, 2015 6:13 pm
by dunbarx
Hi.
Is it that you are simply misusing the word, er, "word"? Please reread all the earlier posts. It can be said that "words" are, by default, delimited by spaces. That is not negotiable. But items can be delimited by anything, and as many have pointed out, it is simple to create a string where the items are nothing like the words. They may well be mutually exclusive.
Craig
Re: String parsing
Posted: Tue Apr 21, 2015 11:30 pm
by drhoward
My thanks to all of you. This feature was not available in programming languages that I've used in the past. It's been over 30 years since I programmed professionally, and I had not used a stack based language of this type.