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
reading in text from a file and filling arrays
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 1
- Joined: Tue Dec 16, 2014 10:57 pm
Re: reading in text from a file and filling arrays
Looks like you should be able to use the "split" command
http://docs.runrev.com/Command/split
So, something like...
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]
-
- VIP Livecode Opensource Backer
- Posts: 4003
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: reading in text from a file and filling arrays
Hi Smokey,
welcome to the Forum.
4 separate arrays would be:
a very fast variant of the repeat loop is repeat for each (-> dictionary)
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
welcome to the Forum.
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 500C001 F3 10 S
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
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
Or I did not understand well what you want to do
Kind regards
Bernd
Re: reading in text from a file and filling arrays
Asuming the data are delimited by space as in your post:
and this for the txt
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
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.