Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems Parsing a .csv file

Status
Not open for further replies.

drichter12

Technical User
Dec 15, 2005
232
US
I am trying to parse a line in a .csv file and break it into 3 variables. Here is the line...

172 0 01 10,70487,914193890129

The variables should be

v1 = 172 0 01 10
v2 = 70487
v3 = 914193890129

These variables will then be entered at a command line 1 item at a time with a carriage return after each. My problem is getting these variables to parse correctly as I seem to have issues related to the fact that the first variable has spaces in it. Can anyone point me in the right direction?

Thanks,


Dale
 
Oh.. :-) Sorry, I missed the "," between variables... New to programming. Thank you soo much! It all appears to be working now.



Dale
 
To process a CSV line with any number of columns I would rather parse the columns into stem:
Code:
[COLOR=#0000ff]/* -- main program -- */[/color]
csv_line1 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'172 0 01 10,70487,914193890129'[/color]
[COLOR=#804040][b]call[/b][/color][COLOR=#008080] csvline_to_stem([/color]csv_line1[COLOR=#008080])[/color]
[COLOR=#804040][b]call[/b][/color][COLOR=#008080] print_stem[/color]
[COLOR=#804040][b]say[/b][/color] [COLOR=#ff00ff]'----------------------'[/color]
csv_line2 [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'172 0 01 10,70487,914193890129,172 0 01 10,70487,914193890129'[/color]
[COLOR=#804040][b]call[/b][/color][COLOR=#008080] csvline_to_stem([/color]csv_line2[COLOR=#008080])[/color]
[COLOR=#804040][b]call[/b][/color][COLOR=#008080] print_stem[/color]
[COLOR=#804040][b]exit[/b][/color]

[COLOR=#0000ff]/* -- procedures/functions -- */[/color]
[COLOR=#008080]csvline_to_stem[/color]: [COLOR=#804040][b]procedure expose[/b][/color] elements.
  [COLOR=#0000ff]/* parse CSV line into global stem */[/color]
  [COLOR=#804040][b]parse arg[/b][/color] csv_line
  [COLOR=#0000ff]/* replace space with '*' and comma with space */[/color] 
  csv_line [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]translate([/color]csv_line[COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]'* '[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]' ,'[/color][COLOR=#008080])[/color]  
  elements.0 [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]words([/color]csv_line[COLOR=#008080])[/color]
  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color]1[COLOR=#804040][b] to [/b][/color]elements.0
    [COLOR=#0000ff]/* replace '*' with space */[/color]
    elements.i [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]translate(word([/color]csv_line[COLOR=#804040][b],[/b][/color] i[COLOR=#008080])[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]' '[/color][COLOR=#804040][b],[/b][/color] [COLOR=#ff00ff]'*'[/color][COLOR=#008080])[/color]
  [COLOR=#804040][b]end[/b][/color]
  [COLOR=#804040][b]return[/b][/color]

[COLOR=#008080]print_stem[/color]: [COLOR=#804040][b]procedure expose[/b][/color] elements.
  [COLOR=#0000ff]/* print global stem */[/color]
  [COLOR=#804040][b]say[/b][/color] [COLOR=#ff00ff]'Global stem has'[/color] elements.0 [COLOR=#ff00ff]'elements:'[/color]
  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color]1[COLOR=#804040][b] to [/b][/color]elements.0
    [COLOR=#804040][b]say[/b][/color] [COLOR=#ff00ff]'elements['[/color][COLOR=#804040][b]||[/b][/color]i[COLOR=#804040][b]||[/b][/color][COLOR=#ff00ff]']='[/color] elements.i
  [COLOR=#804040][b]end[/b][/color]
  [COLOR=#804040][b]return[/b][/color]
Output:
Code:
Global stem has 3 elements:
elements[1]= 172 0 01 10
elements[2]= 70487
elements[3]= 914193890129
----------------------
Global stem has 6 elements:
elements[1]= 172 0 01 10
elements[2]= 70487
elements[3]= 914193890129
elements[4]= 172 0 01 10
elements[5]= 70487
elements[6]= 914193890129
 
Ouch, that makes my head hurt :-)

This is what I ended up with...

/* Create New Phantom Extensions from .csv file 04-29-2010*/

file = "c:\ACDtoPHANTOM.csv"

do while( lines(file) )

data=LINEIN(file)

Parse Var data v1","v2","v3"

CALL ZocTimeout 60
CALL ZocWaitForSeq 1

CALL ZocSend "new^M"

CALL ZocWait "TYPE:"
CALL ZocSend "500^M"

CALL ZocWait "TN"
CALL ZocSend v1"^M"

CALL ZocWait "CDEN"
CALL ZocSend "^M"

CALL ZocWait "DES"
CALL ZocSend "SPDCL^M"

CALL ZocWait "CUST"
CALL ZocSend "0^M"

CALL ZocWait "ERL"
CALL ZocSend "^M"

CALL ZocWait "DN"
CALL ZocSend v2"^M"

CALL ZocWait "CPND"
CALL ZocSend "^M"

CALL ZocWait "VMB"
CALL ZocSend "^M"

CALL ZocWait "HUNT"
CALL ZocSend "^M"

CALL ZocWait "TGAR"
CALL ZocSend "^M"

CALL ZocWait "LDN"
CALL ZocSend "^M"

CALL ZocWait "NCOS"
CALL ZocSend "^M"

CALL ZocWait "RNPG"
CALL ZocSend "^M"

CALL ZocWait "SCPW"
CALL ZocSend "^M"

CALL ZocWait "SGRP"
CALL ZocSend "^M"

CALL ZocWait "CLS"
CALL ZocSend "xfa cfxa^M"

CALL ZocWait "MLWU_LANG"
CALL ZocSend "^M"

CALL ZocWait "PLEV"
CALL ZocSend "^M"

CALL ZocWait "FTR"
CALL ZocSend "dcfw 16 "v3"^M"

CALL ZocWait "FTR"
CALL ZocSend "^M"

CALL ZocWait "REQ"

END



Dale
 
Dale,

You must be using ZOC to excute scripts on your Nortel? Good to see!!!
 
Yes I am. I switched to ZOC last year to get away from ProComm since it hasn't been supported for close to 10 years now. So far I have been thrilled with it :-)



Dale
 
Are you pulling your .csv file from the MV.xls utility?
 
I'm using the updated version from GHTROUT's site buy yes most of the time depending on what I need....



Dale
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top