Parsing files

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
carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Parsing files

Post by carel » Tue Sep 10, 2013 3:18 pm

I'm saving user settings to a text file for example:
value1
value2
value3
value4

But sometimes the values are empty or I get a return in when I save it, then valuex is not where it is supposed to be. Does anybody have an example of parsing a file that would look like this:

var1=value1
var2=value2
var4=value3

etc...

Thanks,

Carel

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Parsing files

Post by Klaus » Tue Sep 10, 2013 3:42 pm

Hi Carel,

well, we need to know what your are parsing the file for, how do you want to use the result of the "parsing"!
"Parsing" a CR delimited file is easy :D


Best

Klaus

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Parsing files

Post by carel » Tue Sep 10, 2013 4:06 pm

Hi Klaus,

I store settings for a little program I'm writing for a school for example LessonName, WorkingFolder etc.

I think it would be better if I put the variable name in the file as well, so that I don't assign the wrong value to the wrong variable. I think I'll manage to get it right but I just want an example, otherwise I'm going to write 50 lines of code that can be achieved in 5 lines :mrgreen:

Thanks,

Carel

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Parsing files

Post by Klaus » Tue Sep 10, 2013 4:14 pm

Hi Carel,

ah, you want to put "value1" into the variable named "var1" :D
Since this is not directly possible***, you need to use DO:
***You can not acces a variable with its name if this name is in another variable!

Code: Select all

...
put url("file:" & "your path/to/that/namely/file.txt") into into tList

## We want to use the name of the variable AND its value so:
set itemdel to "="
repeat for each line tLine in tList

  ## See, the ITEMDELIMITER is our friend here, too :-)
  put item 1 of tLine into tVariableName
  put item 2 of tLine into tValue
  do "put tValue into tVariableName"
end repeat
...
Now you have all the correct variabels with their correct values.
var1 -> value1
var2 -> value2
etc...

Do you need these varaibles as GLOBALS or LOCALS?
In that case we will need to add a line...


Best

Klaus

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

Re: Parsing files

Post by FourthWorld » Tue Sep 10, 2013 4:28 pm

And for those of us cranky folks who get too easily freaked out by "do", you could also use one array variable. Arrays can be a good choice when you need a number of variables whose names can't be known in advance, putting them all into one container consisting of name-value pairs:

Code: Select all

global gPrefsA
put url ("file: "& tSomeFilePath) into gPrefsA
split gPrefsA by cr and "="
To access it:

Code: Select all

global gPrefsA
get gPrefsA["var1"]
If you need to save the values back out to disk you can use:

Code: Select all

global gPrefsA
combine gPrefsA with cr and "="
put gPrefsA into url ("file:" & tSomeFilePath)
As Klaus alluded to, the global declaration assumes you'll be using the values throughout the session. If that's not the case it can be safely omitted. If you need to use a global var and all the handlers that access it are in one script, you can instead declare it once outside of the handlers at the top of the script, instead of having to declare it in each handler that uses it.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

carel
Posts: 58
Joined: Mon Jul 29, 2013 2:49 pm

Re: Parsing files

Post by carel » Tue Sep 10, 2013 4:57 pm

Thanks Klaus and Richard,

I'll play around with you examples and see what works best for me. Do I still have to open the file beforehand?

Thanks about the globals too Richard, I've been declaring it in every handler.

And to just prove my point about the 50 lines vs 5 lines, this is the code that I'll soon replace:

if there is a file tFilePath then
open file tFilePath for read
read from file tFilePath for 1 line
put it into gWorkingFolder
read from file tFilePath for 1 line
put it into gServerSpeed
read from file tFilePath for 1 line
put it into gUseServerLesson
read from file tFilePath for 1 line
put it into gUseServerSpeed
read from file tFilePath for 1 line
put it into gCurrentLesson
read from file tFilePath for 1 line
put it into gLocalSpeed
read from file tFilePath for 1 line
put it into gServerLesson
close file tFilePath
else

Thanks,

Carel

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Parsing files

Post by Klaus » Tue Sep 10, 2013 5:06 pm

Hi Carel,

it it much more effective when you read the complete file at once, put it into a vaiable and then process that variable!

Use the URL syntax for this:

Code: Select all

...
## I avoid any nested IF THEN clauses if possible :-)
if there is NOT a file tFilePath then
  exit to top
end if

put url("file:" & tFilePath) into tList
## Now do something with all lines of tList :-)
...
Best

Klaus

Post Reply