Page 1 of 1
working with files.
Posted: Mon Jul 01, 2013 9:53 pm
by kimberlyn
Hello everybody !
I just started working on livecode but i have some troubles when working with files.
To make it simple, the format of every line of my document are like :
line 1 : "xxxxx,yyyy,zzzzz,nb1,nb2"
line 2 : "aaaaa,bbb,ccccc,nb3,nb4"
and i would like to know how to take for example the strings "yyyy" and "bbb" to make some work on them (compare, modify etc...).
I've thought about putting every line in a table, and then take the "2nde" value, but maybe there is a function which can help ? (I've only made C programming before... quite different right ?). I have the ideas but programming is quite difficult :S
Thanks a lot for your help

Re: working with files.
Posted: Mon Jul 01, 2013 11:27 pm
by Dixie
Not too sure what you are after exactly but if the data was something like this :-
abc, John, vdr,223,222
ser, peter,d dd,212,998
see, John, fdd,333,444
saw, john, ggf,233,554
Then
Code: Select all
on mouseUp
repeat with count = 1 to the number of lines of fld 1
if item 2 of line count of fld 1 = item 2 of line (count + 1) of fld 1 then
put count && count + 1
end if
end repeat
end mouseUp
the message box would contain 3 4, as 'John' appeared in item 2 of lines 3 and 4 of the data...
Re: working with files.
Posted: Mon Jul 01, 2013 11:29 pm
by Simon
Hi kimberlyn,
Welcome to the forum!
Check out itemDel in the dictionary.
It's default is comma so you're set.
Code: Select all
put item 2 of line 1 of myText into myVar
And there you go.
Simon
Re: working with files.
Posted: Tue Jul 02, 2013 2:23 pm
by kimberlyn
Hi !
Thanks a lot for your answers. It helped me.
Now that I'm here :p I have for example lines in a text file like this :
My; name; is; kimberlyn; [cookies, natation, greece];
Your; name; is; joey; [ice cream; basketball; australia];
...
and so on.....
And I want to split them and put the elements into a 2D array.
At the beginning when i want to split the lines
split input by "];"
doesn't work because in the dictionnary they said that the argument can only be ONE character... so i don't know if there is another magical word or if i need to do another way... this is my only problem.
Sorry for the presentation

Re: working with files.
Posted: Tue Jul 02, 2013 9:44 pm
by Simon
Hi kimberlyn,
I'm unclear as to what your 2D array should look like?
Code: Select all
global tMultiArray
on mouseUp
put "My; name; is; kimberlyn; " into tName
put "cookies, natation, greece;" into tSelect
put "" into tMultiArray
repeat for each word tWord in tName
add 1 to x
put word x of tSelect into tChoice
put tWord into tMultiArray[tWord][tChoice]
end repeat
end mouseUp
is just silly. Once you run that and see what it produces, describe what it was you wanted.
Simon
Re: working with files.
Posted: Wed Jul 03, 2013 9:53 am
by kimberlyn
In fact my file contains ligns of data like this.
Code: Select all
ITEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0];
ITEM2,1,0,,ATK,DFS,SPE,[GDF,30,60,1];
but sometimes they are "cut" like this
Code: Select all
ITEM22,4,3,5,ATK,DFS,SPE,[GJF,
30,1200,300];
ITEM23,1,0,,ATK,DFS,SPE,[GHF,
100,0,10];
and my code is :
Code: Select all
open file File for read
read from file File until EOF
put it into input
split input by "];"
repeat with i = 1 to the number of lines in the keys of input
split input[i] by "," --split each line with ","
put input[i][1] into var1
put input[i][2] into var2
put input[i][7] into var3
end repeat
The problem is at the line
split input by "];" because we can split with only one character. And
split input by return is not adapted here

(((
Do you understand ?
Re: working with files.
Posted: Wed Jul 03, 2013 11:25 am
by shaosean
Set the
lineDelimiter property before you run your loop
Code: Select all
open file File for read
read from file File until EOF
put it into input
-- split input by "];"
set the lineDelimiter to ";"
repeat with i = 1 to the number of lines in the keys of input
split input[i] by "," --split each line with ","
put input[i][1] into var1
put input[i][2] into var2
put input[i][7] into var3
end repeat
Re: working with files.
Posted: Wed Jul 03, 2013 11:42 am
by bn
Hi Kimberlyn,
in order to clean up your data here is a way to use the lineDelimiter as Shao suggested
Code: Select all
on mouseUp
put field "fOrigData" into tData
replace cr with "" in tData -- get rid of returns otherwise they show up in the data
set the lineDelimiter to ";" -- define line delimiter
repeat for each line aLine in tData -- the variable aLine holds one line of tData, i.e. the content of ;y,,mdfa as; not including the delimiter
put aLIne & cr after tCollect -- assemble the new cleaned up version of your data, no returns and no ";" in it
end repeat
delete last char of tCollect -- a return
put tCollect into field "fCleanedData" -- now work on your data
end mouseUp
You now have a cleaned up version of your data, no returns and no ";" in it.
As for the array I don't see what should be the structure of the array
I guess the key would be "item1" etc?
What should go into the array as a value, how many subarrays?
Kind regards
Bernd
Re: working with files.
Posted: Wed Jul 03, 2013 12:33 pm
by kimberlyn
Well, while you helped me I worked and I also put the delimiter as ";". It works fine for this file.
But it's not finished yet

In fact I use two files, and the second one is a bit different in its content :
Code: Select all
ITEM1;10;3;50;ATK;DFS;SPE;[GOF,30,33,0]; --it's an example
In this case I cannot use the delimiter as ";" ! That's why I wanted to split with
"];" !
And my goal is to merge some of the parameters. For example in this case, I want to put in an array "ITEM1ATK30". (1st + 5th + 9th parameters)
Re: working with files.
Posted: Wed Jul 03, 2013 12:54 pm
by bn
try this
Code: Select all
on mouseUp
put field "fOrigData" into tData
replace cr with "" in tData -- get rid of returns otherwise they show up in the data
put numToChar(202) into tPlaceHolder -- just temporary delimiter
replace "];" with tPlaceHolder in tData
set the lineDelimiter to tPlaceHolder -- define line delimiter
repeat for each line aLine in tData -- the variable aLine holds one line of tData, i.e. the content of ;y,,mdfa as; not including the delimiter
if ";" is in aLine then replace ";" with comma in aLine -- clean ";"
put aLIne & "]" & cr after tCollect -- assemble the new cleaned up version of your data, no returns and no ";" in it
end repeat
delete last char of tCollect -- a return
put tCollect into field "fCleanedData" -- now work on your data
end mouseUp
now come up with the the curly quotes "{} that at times are used instead of "[]"

Is your datasource some sort of random generator?
Kind regards
Bernd
Re: working with files.
Posted: Wed Jul 03, 2013 1:57 pm
by bn
with this set of data in field "fOrigData":
Code: Select all
ITEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0];
ITEM2,1,0,,ATK,DFS,SPE,[GDF,30,60,1];
ITEM22,4,3,5,ATK,DFS,SPE,[GJF,
30,1200,300];
ITEM23,1,0,,ATK,DFS,SPE,[GHF,
100,0,10];
ITEM11;10;3;50;ATK;DFS;SPE;[GOF,30,33,0];
using this code in a button
Code: Select all
on mouseUp
put field "fOrigData" into tData
replace cr with "" in tData -- get rid of returns otherwise they show up in the data
put numToChar(202) into tPlaceHolder -- just temporary delimiter
replace "];" with tPlaceHolder in tData
set the lineDelimiter to tPlaceHolder -- define line delimiter
repeat for each line aLine in tData -- the variable aLine holds one line of tData, i.e. the content of ;y,,mdfa as; not including the delimiter
if ";" is in aLine then replace ";" with comma in aLine
put aLIne & "]" & cr after tCollect -- assemble the new cleaned up version of your data, no returns and no ";" in it
end repeat
delete last char of tCollect -- a return
put tCollect into field "fCleanedData" -- now work on your data
set the lineDelimiter to cr
-- do the array
put "" into tArray
replace "[" with "" in tCollect -- in case you dont want these
replace "]" with "" in tCollect -- in case you dont want these
repeat for each line aLine in tCollect
put item 1 of aLIne into tKey
repeat with i = 2 to the number of items in aLine
put item i of aLine into tArray[tKey][i]
end repeat
end repeat
-- end array building
-- now get item 1,5,9 for every key
put "" into tCollect -- clear variable
put the keys of tArray into theKeys
-- possibly sort the the keys here
repeat for each line aLine in theKeys
put aLine & tArray[aLIne][5] & tArray[aLine][9] & cr after tCollect
end repeat
delete last char of tCollect -- a return
put tCollect into field "fRes"
end mouseUp
I get in field "fCleanedData"
Code: Select all
ITEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0]
ITEM2,1,0,,ATK,DFS,SPE,[GDF,30,60,1]
ITEM22,4,3,5,ATK,DFS,SPE,[GJF,30,1200,300]
ITEM23,1,0,,ATK,DFS,SPE,[GHF,100,0,10]
ITEM11,10,3,50,ATK,DFS,SPE,[GOF,30,33,0]
and this in field "fRes" for item 1,5,9 of each line:
Code: Select all
ITEM1ATK30
ITEM11ATK30
ITEM2ATK30
ITEM22ATK30
ITEM23ATK100
Kind regards
Bernd
edited original data since there were 2 lines that started with "item1" and thus lead to only 4 resulting lines after array treatment since the keys were not unique.
Re: working with files.
Posted: Wed Jul 03, 2013 3:36 pm
by shaosean
In this case I cannot use the delimiter as ";" ! That's why I wanted to split with "];" !
you could always do a replace on those characters with an unprintable character and use that as your line delimiter..
Code: Select all
replace "];" with ("];" & numToChar(30)) in tData
set the lineDelimiter to numToChar(30)
Re: working with files.
Posted: Wed Jul 03, 2013 5:18 pm
by JGonz
Hi,
coming from the database side of coding I'm asking myself what's it that we see here:
My; name; is; kimberlyn; [cookies, natation, greece];
or:
TEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0];
That's, for me, strange things. No clearly structured data, but strange voodoo that could be avoided easily using openly available and well tested tools - databases, as we use to call them.
I'm coming from the stone age of HyperCard, but even back then we used clearly defined structures. Because they just work.
A record is a line.
A field in a record is an item of the line.
There's no 2 records in 1 record.
If you need this, google "relational".
You may make relations between different "tables" (files), using indexoid structures and either text or binary files.
And you may code all of this in LC. Back then, I made a (basic) relational database engine in HyperCard, with heavy use of CompileIt! Xternals that did the read/write/search operations. This isn't necessary anymore today, our computers are fast enough that no assembler optimization is needed anymore.
But you need, now as well as ages ago, a well thought out and working data model to do the job.
This:
TEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0];
doesn't look to me like such. What's the data in the brackets? If the data belong to the record, why in brackets? If the data in brackets don't belong to the record, why are they there at all?
In this case:
0001,TEM1,10,3,50,ATK,DFS,SPE
and
0013,GOF,30,33,0,0001
where the last item in the second record points to the first record (for sure, both in different tables)?
But I might be completely wrong, not understanding at all what you're asking for - in this case I'm asking for pardon, and for a merciful ignoring of this post.
Thx for reading, and have a good time!
Edit: Killed a bracket where it didn't belong to
Re: working with files.
Posted: Wed Jul 03, 2013 8:56 pm
by kimberlyn
bn wrote:with this set of data in field "fOrigData":
Code: Select all
ITEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0];
ITEM2,1,0,,ATK,DFS,SPE,[GDF,30,60,1];
ITEM22,4,3,5,ATK,DFS,SPE,[GJF,
30,1200,300];
ITEM23,1,0,,ATK,DFS,SPE,[GHF,
100,0,10];
ITEM11;10;3;50;ATK;DFS;SPE;[GOF,30,33,0];
using this code in a button
Code: Select all
on mouseUp
put field "fOrigData" into tData
replace cr with "" in tData -- get rid of returns otherwise they show up in the data
put numToChar(202) into tPlaceHolder -- just temporary delimiter
replace "];" with tPlaceHolder in tData
set the lineDelimiter to tPlaceHolder -- define line delimiter
repeat for each line aLine in tData -- the variable aLine holds one line of tData, i.e. the content of ;y,,mdfa as; not including the delimiter
if ";" is in aLine then replace ";" with comma in aLine
put aLIne & "]" & cr after tCollect -- assemble the new cleaned up version of your data, no returns and no ";" in it
end repeat
delete last char of tCollect -- a return
put tCollect into field "fCleanedData" -- now work on your data
set the lineDelimiter to cr
-- do the array
put "" into tArray
replace "[" with "" in tCollect -- in case you dont want these
replace "]" with "" in tCollect -- in case you dont want these
repeat for each line aLine in tCollect
put item 1 of aLIne into tKey
repeat with i = 2 to the number of items in aLine
put item i of aLine into tArray[tKey][i]
end repeat
end repeat
-- end array building
-- now get item 1,5,9 for every key
put "" into tCollect -- clear variable
put the keys of tArray into theKeys
-- possibly sort the the keys here
repeat for each line aLine in theKeys
put aLine & tArray[aLIne][5] & tArray[aLine][9] & cr after tCollect
end repeat
delete last char of tCollect -- a return
put tCollect into field "fRes"
end mouseUp
I get in field "fCleanedData"
Code: Select all
ITEM1,10,3,50,ATK,DFS,SPE,[GOF,30,33,0]
ITEM2,1,0,,ATK,DFS,SPE,[GDF,30,60,1]
ITEM22,4,3,5,ATK,DFS,SPE,[GJF,30,1200,300]
ITEM23,1,0,,ATK,DFS,SPE,[GHF,100,0,10]
ITEM11,10,3,50,ATK,DFS,SPE,[GOF,30,33,0]
and this in field "fRes" for item 1,5,9 of each line:
Code: Select all
ITEM1ATK30
ITEM11ATK30
ITEM2ATK30
ITEM22ATK30
ITEM23ATK100
Kind regards
Bernd
edited original data since there were 2 lines that started with "item1" and thus lead to only 4 resulting lines after array treatment since the keys were not unique.
bn, thank you very very much. I'll win so much time thanks to you
