reading in text from a file and filling arrays

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
smokeymurray
Posts: 1
Joined: Tue Dec 16, 2014 10:57 pm

reading in text from a file and filling arrays

Post by smokeymurray » Tue Dec 16, 2014 11:06 pm

I have set up arrays containing 500 elements to allow me to read in data from a file.

An extraction of the one part of the data is below. There are 500 of these.

C001 F3 10 S


How do I separate the 4 elements and put them into 4 arrays? So, C001 into one array, F3 into another, 10, another, and S, into the final array.

That dat awas taken froma CSV file, however I also have the data in a txt file written like:

COO1,F3,10,S

Any advice is appreciated.

Thanks

mattmaier
Posts: 109
Joined: Fri Apr 19, 2013 2:49 am

Re: reading in text from a file and filling arrays

Post by mattmaier » Tue Dec 16, 2014 11:59 pm

Looks like you should be able to use the "split" command
http://docs.runrev.com/Command/split

So, something like...

Code: Select all

put arrayWhatever[someKey] into tList
split tList by space
put tList into arrayWhatever[someKey]

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3361
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: reading in text from a file and filling arrays

Post by bn » Wed Dec 17, 2014 12:11 am

Hi Smokey,

welcome to the Forum.
C001 F3 10 S
suppose that above is the content of one key of your array of 500 overall and that the keys of the array are numbers from 1 to 500


4 separate arrays would be:

Code: Select all

repeat with x = 1 to 500
put myBigArray[x] into aLine
put word 1 of aLine into tArrayForWord1[x]
put word 2 of aLine into tArrayForWord2[x]
put word 3 of aLine into tArrayForWord3[x]
put word 4 of aLine into tArrayForWord4[x]
end repeat
a very fast variant of the repeat loop is repeat for each (-> dictionary)

Code: Select all

repeat for each key aKey in myBigArray
put myBigArray[aKey] into aLine
put word 1 of aLine into tArrayForWord1[aKey]
put word 2 of aLine into tArrayForWord2[aKey]
put word 3 of aLine into tArrayForWord3[aKey]
put word 4 of aLine into tArrayForWord4[aKey]
end repeat
If you want to use the comma delimited list you would say item x instead of word x. The rest is as above

Or I did not understand well what you want to do

Kind regards
Bernd

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm
Location: Greece

Re: reading in text from a file and filling arrays

Post by zaxos » Wed Dec 17, 2014 12:35 am

Asuming the data are delimited by space as in your post:

Code: Select all

repeat for each line theLine in theData
set itemDel to space
add 1 to x
put item 1 of theLine into tData[x]["Data1"]
put item 2 of theLine into tData[x]["Data2"]
put item 3 of theLine into tData[x]["Data3"]
put item 4 of theLine into tData[x]["Data4"]
end repeat
and this for the txt

Code: Select all

repeat for each line theLine in theData
set itemDel to comma
add 1 to x
put item 1 of theLine into tData[x]["Data1"]
put item 2 of theLine into tData[x]["Data2"]
put item 3 of theLine into tData[x]["Data3"]
put item 4 of theLine into tData[x]["Data4"]
end repeat
Knowledge is meant to be shared.

Post Reply

Return to “Talking LiveCode”