Page 1 of 1
Easiest way get delimited data (CSV) out of file? - Solved
Posted: Sat Nov 30, 2013 10:40 pm
by DR White
So, with help, I finally got some simple text into a text file like below:
Cat,black,four legs,long tail,8
Dog,brown,four legs,short tail,4
What is the easiest way to get delimited data out of a file into groups of 7 variables?
Thanks,
David
Re: Easiest way to get delimited data (CSV) out of a file?
Posted: Sun Dec 01, 2013 12:01 am
by dunbarx
Hi.
The examples you gave had seven items in each line. You must think of these strings as comma delimited and then return delimited.
But you asked for a way to get groups of five variables. Does this mean multiple lines of five items in a line? If the data comes over as comma delimited words separated by returns, think about this sort of thing, with your data in a field "yourDataField":
Code: Select all
on mouseup
get fld YourDataField
repeat with y = 1 to the number of items in it step 5
put item y to y + 4 of it & return after temp
end repeat
answer temp
end mouseup
Where did the returns in the original go? Is it important to know if there are less than five items in the last list?
A great exercise for you would be to find a few other ways to separate the data, maybe with two nested repeat loops. This provided I have even gotten close to what you wanted.
Craig Newman
Re: Easiest way to get delimited data (CSV) out of a file?
Posted: Sun Dec 01, 2013 12:29 am
by DR White
Craig,
It was my mistake. I need to put the data from file, in groups of 7 variables.
Re: Easiest way to get delimited data (CSV) out of a file?
Posted: Sun Dec 01, 2013 1:44 am
by DR White
Craig,
The routine is actually intended display the top scores of a game
File data below:
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
File data needs to come out and go into individual variables:
S1,T1,U1,V1,W1
S2,T2,U2,V2,W2
S3,T3,U3,V3,W3
S4,T4,U4,V4,W4
S5,T5,U5,V5,W5
S6,T6,U6,V6,W6
S7,T7,U7,V7,W7
S8,T8,U8,V8,W8
S9,T9,U9,V9,W9
S10,T10,U10,V10,W10
I have searched the dictionary for clues, but I have not found a way to separate the data out into individual variables.
Any help from anyone will be appreciated,
David
Re: Easiest way to get delimited data (CSV) out of a file?
Posted: Sun Dec 01, 2013 2:40 am
by Simon
Here are some clues;
a,b,c,d,e
f,g,h,i,j
item 2 of line 1 is "b"
item 3 of line 1 is "c"
item 1 of line 2 is "f"
item 4 of line 2 is "i"
You can repeat through each line then through each item.
itemDelimeter defaults to "," (good for you)
Now you said "groups" the first time, but I see 50 variables is that really what you want?
Simon
Re: Easiest way to get delimited data (CSV) out of a file?
Posted: Sun Dec 01, 2013 1:08 pm
by DR White
Simon,
The data needs to come out in 10 groups of 5 variables each.
Thanks,
David
Re: Easiest way to get delimited data (CSV) out of a file?
Posted: Sun Dec 01, 2013 1:21 pm
by Klaus
Hi David,
situation like these where you do not know how much variables you may need to fill with whatever content,
I would strongly recommend using an ARRAY!
Like this with your first sample data:
Cat,black,four legs,long tail,8
Dog,brown,four legs,short tail,4
...
In my example this list in in the variable tLis:
Code: Select all
...
## We fill an multidimensional array:
## Since I will use REPEAT FOR EACH, we need to manage a counter ourselves:
put 1 into tCounter
## We go through all lines and put the items form ine line into the array!
## I "made up" the NAMES for the key, you get the picture
repeat for each line tLine in tList
put item 1 of tLine into tArray[tCounter]["animal"]
put item 2 of tLine into tArray[tCounter]["color"]
put item 3 of tLine into tArray[tCounter]["leg"]
put item 4 of tLine into tArray[tCounter]["tail"]
put item 5 of tLine into tArray[tCounter]["your_number"]
## Manage counter:
add 1 to tCounter
end repeat
...
Now we have all data in this array and you can access it with:
Code: Select all
...
answer "The first animal is" && ttArray[1]["name"]
...
Make the array global and that's it
Best
Klaus
Re: Easiest way to get delimited data (CSV) out of a file?
Posted: Sun Dec 01, 2013 2:16 pm
by DR White
Klaus,
It is an array and I know how to work with arrays.
The problem is after saving the array in a text file as shown post Sat Nov 30, 2013 7:44 pm,
The routine is actually intended display the top scores of a game
File data below:
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
a,b,c,d,e
File data needs to come out and go into individual variables:
S1,T1,U1,V1,W1
S2,T2,U2,V2,W2
S3,T3,U3,V3,W3
S4,T4,U4,V4,W4
S5,T5,U5,V5,W5
S6,T6,U6,V6,W6
S7,T7,U7,V7,W7
S8,T8,U8,V8,W8
S9,T9,U9,V9,W9
S10,T10,U10,V10,W10
I don't know how to take the 50 items of data that come out of the file as one variable with commas between the items and put them back into an array of 50 variables.
Thanks,
David
Re: Easiest way to get delimited data (CSV) out of a file?
Posted: Sun Dec 01, 2013 4:39 pm
by DR White
Klaus,
I did not know how to use the "Item" instruction.
That should work.
Thanks,
David
Re: Easiest way to get delimited data (CSV) out of a file?
Posted: Sun Dec 01, 2013 7:06 pm
by DR White
Klaus,
Works Great!
Thanks,
David