Page 1 of 1
reading in text from a file and filling arrays
Posted: Tue Dec 16, 2014 11:06 pm
by smokeymurray
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
Re: reading in text from a file and filling arrays
Posted: Tue Dec 16, 2014 11:59 pm
by mattmaier
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]
Re: reading in text from a file and filling arrays
Posted: Wed Dec 17, 2014 12:11 am
by bn
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
Re: reading in text from a file and filling arrays
Posted: Wed Dec 17, 2014 12:35 am
by zaxos
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