View previous topic :: View next topic |
Author |
Message |
bjdavis Valued Newbie
Joined: 03 Jan 2002 Posts: 28 Location: Jacksonville, FL
|
Posted: Wed Mar 20, 2002 6:09 pm Post subject: Random amount of variables |
|
|
I am trying to create a program that reads a *.csv file
Each line contains different amount of commas
(i.e.
example.csv
This,is,line,1,and,it,has,10,commas,in,it
This,is,line,2,and,the,commas,in,this,line,equal,to,12
)
I am able to parse through each line if I create a parse command as such
Code: |
list create,1
list loadfile, 1, example.csv
If @Greater(@count(1),0)
List Seek,1,0
repeat
Parse("%%L1";"%%L2";"%%L3";"%%L4";"%%L5";"%%L6";"%%L7";"%%L8";"%%L9";"%%L10";"%%L11";"%%L12";"%%L13";"%%L14";"%%L15"),@item(1)
Info %%L1 %%L2 %%L3 %%L4 %%L5 %%L6 %%L7 %%L8 %%L9 %%L10 %%L11 %%L12 %%L13 %%L14 %%L15
Until @Not(@item(1))
info DONE !
End
|
Now my delima is this. The *.csv file changes each day.
And it will have lines in it with more than 15 commas.
The exact amount I will not know before hand.
So I figure I could count the amount of commas before I parse the line.
Code: |
%%CountCommas = 0
%%Step = 1
list create,1
list loadfile, 1, example.csv
If @Greater(@count(1),0)
List Seek,1,0
Repeat
%%Line = @Item(1)
Repeat
%C = @Substr(%%Line,%%Step,%%Step)
If @Equal(%C,",")
%%CountCommas = @Succ(%%CountCommas)
End
%%Step = @Succ(%%Step)
Info There are %%CountCommas Commas in this Line
Until @Equal(%%Step,@Len(%%Line))
Until @Not(@Item(1))
Info Done!
End
|
So instead of trying to create a Parse statement with 50+ variables is there a way to create the correct amount of variables
in a variable set and put it into a Parse statement?
(If that makes any since???)
Any help would be appreciated.
Thanks in advance. _________________ B.J. Davis, MCSE |
|
Back to top |
|
|
Dr. Dread Professional Member
Joined: 03 Aug 2001 Posts: 1065 Location: Copenhagen, Denmark
|
Posted: Wed Mar 20, 2002 7:17 pm Post subject: |
|
|
I gave your comma-counting routine a spin. I think that it just needs a
rearrangement to make the right counting increments and to reset the
counters in the loop.
This should work:
Code: |
%%string = "This,is,line,1,and,it,has,10,commas,in,it"
%%string2 = "this,is,line,2,and,the,commas,in,this,line,equal,to,12"
list create,1
list add, 1,%%string
list add, 1,%%string2
If @Greater(@count(1),0)
List Seek,1,0
%%linecount = 0
Repeat
%%Line = @next(1)
%%Step = 1
%%CountCommas = 0
Repeat
%C = @Substr(%%Line,%%Step,%%Step)
If @Equal(%C,",")
%%CountCommas = @Succ(%%CountCommas)
End
%%Step = @Succ(%%Step)
Until @equal(%%Step,@Len(%%Line))
Info There are %%CountCommas Commas in this Line
Until @equal(@Index(1),@Count(1))
End |
Perhaps that will take you a step further.
Greetz
Dread _________________ ~~ Alcohol and calculus don't mix... Don't drink and derive! ~~
String.DLL * advanced string processing |
|
Back to top |
|
|
LiquidCode Moderator Team
Joined: 05 Dec 2000 Posts: 1751 Location: Space and Time
|
Posted: Wed Mar 20, 2002 7:32 pm Post subject: |
|
|
Ok, Here is what I do sometimes. You can make a find and replace
function (I have one I will post later if you want), and replace the
commas in the string with @chr(13)@chr(10). This will put each field
on a seperate line. Then you can assign the line to a list and step
through it to get the info. Make sense? I can make a quick sample
if you want, let me know. _________________ Chris
Http://theblindhouse.com |
|
Back to top |
|
|
bjdavis Valued Newbie
Joined: 03 Jan 2002 Posts: 28 Location: Jacksonville, FL
|
Posted: Wed Mar 20, 2002 9:09 pm Post subject: |
|
|
Thanks, for the correction Dr. Dread
Now is the tricky part.
(And LiquidCode's Avator shows exactly what I have been doing)
I need to build a variable set (which I figured out how to do)
and then be able to read those variables.
Code: |
%%string = "This,is,line,1,and,it,has,10,commas,in,it"
%%string2 = "this,is,line,2,and,the,commas,in,this,line,equal,to,12"
list create,1
list add, 1,%%string
list add, 1,%%string2
If @Greater(@count(1),0)
List Seek,1,0
%%linecount = 0
Repeat
%%Line = @next(1)
%%Step = 1
%%CountCommas = 0
Repeat
%C = @Substr(%%Line,%%Step,%%Step)
If @Equal(%C,",")
%%CountCommas = @Succ(%%CountCommas)
End
%%Step = @Succ(%%Step)
Until @equal(%%Step,@Len(%%Line))
Info There are %%CountCommas Commas in this Line
Until @equal(@Index(1),@Count(1))
End
REM *************************************************************
REM
REM Build Variable Set and then read each variable value
REM
REM *************************************************************
%P = @chr(37)@chr(37)"Line1"
%%Loop = 2
Repeat
%P = %P";"@chr(37)@chr(37)"Line"%%Loop
%%Loop = @Succ(%%Loop)
Until @Equal(@Succ(%%CountCommas),@pred(%%Loop))
List Seek, list1,0
%L = @Next(List1)
Option Fieldsep, ","
Parse %P, %L
%I = "Item 1 = "@chr(37)@chr(37)"Line1 "
%%Loop = 2
Repeat
%I = %I @cr()"Item "%%Loop" = "@chr(37)@chr(37)"Line"%%Loop
%%Loop = @Succ(%%Loop)
Until @equal(@Pred(%%Loop),@Succ(%%CountCommas))
%I = "List Item value is:"@cr()%L@cr()@cr()"Variable Set:"@cr()%p@cr()@cr()"Values Are:"@cr()%I
Info %I
|
I want to get the following
Item 1 = This
Item 2 = is
Item 3 = line
... so on and so on
But what I get is this
Item 1 = %%Line1
Item 2 = %%Line2
Item 3 = %%Line3
... so on and so on
Any ideas on how to read the variables??? _________________ B.J. Davis, MCSE |
|
Back to top |
|
|
Mac Professional Member
Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Wed Mar 20, 2002 10:06 pm Post subject: |
|
|
Here ya go. If you use a list, you have unimited vars
in incremented order (@item(L1, 1), @item(L1, 2), etc.).
Click on an item in the top list...
Code: |
OPTION SCALE, 96
OPTION DECIMALSEP, "."
TITLE By Mac
DIALOG CREATE,Test prog,-1,0,300,195
DIALOG ADD,LIST,L1,5,5,290,90,,CLICK
DIALOG ADD,LIST,L2,100,5,290,90
DIALOG SHOW
LIST LOADTEXT, L1,
"this, is line one
"this, is, line two
"this, is, line, three
:EVLOOP
WAIT EVENT
goto @event()
:L1CLICK
LIST CLEAR, L2
%s = ""
%x = 1
REPEAT
if @equal(@substr(@item(L1), %x), ",")
LIST ADD, L2, @trim(%s)
%s = ""
else
%s = %s@substr(@item(L1), %x)
end
%x = @succ(%x)
UNTIL @greater(%x, @len(@item(L1)))
LIST ADD, L2, @trim(%s)
goto EVLOOP
:CLOSE
EXIT
|
_________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
|
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|