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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More String Parsing......

Status
Not open for further replies.

grindel

Technical User
Jun 25, 2002
36
CA
Hi All

I'm writing a script that reads a CSV file and outputs an LDIF file for import. My CSV record would look like this:

Test,User,tuser@r.com,123456|1|C1^234567|2|C2^3456789|3|C3

I can "split" out the first 3 fields, but the last one is giving me grief. It's actually 3 separate valued separated by "^" - it may be only 1, it may be 2, it may be 5 values - I just don't know until I read the record. How can I split these values out without knowing exactly how many there are?
TIA
Rick
 
Never mind all - I got the answer from my trusty "Learning Perl" Llama book.
My line looks like:

@array = split(/\^/,$numbers)
where $numbers is the field I'm trying to split up.

Thanks all....
Rick
 
To make things a little cleaner, you could use the transliteration operator to create a common delimiter, then split on that -

Code:
$line = "Test,User,tuser@r.com,123456|1|C1^234567|2|C2^3456789|3|C3";
$line =~ tr/,^/;/; #change all commas and carats to semicolon
@array = split (/;/,$line); #split on semicolon

 
I should have used single quotes here because of the ampersand. (Not an issue if you're reading from a file though.)

$line = 'Test,User,tuser@r.com,123456|1|C1^234567|2|C2^3456789|3|C3';
 
Once the array is built I need to split it up:

@array = split(/\^/,$line);
for($i=0; $i <= $#array; $i++){
print OUT &quot;sk-can: $can_array[$i]\n&quot;;
}
Works just like I'd imagined! :)

Thx...
Rick

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top