String parsing

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

String parsing

Post by drhoward » Tue Apr 21, 2015 9:14 am

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!

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: String parsing

Post by Thierry » Tue Apr 21, 2015 9:18 am

Hi,

You might check for item keyword in the dictionary, but not word.

HTH,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

FredBeck
Posts: 77
Joined: Fri Nov 01, 2013 3:07 pm
Contact:

Re: String parsing

Post by FredBeck » Tue Apr 21, 2015 12:47 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: String parsing

Post by dunbarx » Tue Apr 21, 2015 2:43 pm

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

drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Re: String parsing

Post by drhoward » Tue Apr 21, 2015 3:56 pm

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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: String parsing

Post by FourthWorld » Tue Apr 21, 2015 4:15 pm

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.:

Code: Select all

put item 2 of "25,34,0.23"
...should put "34" into the Message Box.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: String parsing

Post by dunbarx » Tue Apr 21, 2015 6:13 pm

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

drhoward
Posts: 22
Joined: Tue Dec 10, 2013 9:03 am

Re: String parsing

Post by drhoward » Tue Apr 21, 2015 11:30 pm

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.

Post Reply