in reply to split problems
Easy, split by " :)
Ok, this all assumes we don't have embedded parentheses in the quoted fields (If you do, it gets more complicated). But if you have a variable, $row, let's say, with the following value:
1,"Hugo,Inc.",4,"This is a test",34.5, 17.9, "Marc, Cindy and Rob"
Now, if you split this variable on ", and store it in @splitArray, then you will get the following:
$splitArray[0] = 1, $splitArray[1] = Hugo, Inc. $splitArray[2] = ,4, $splitArray[3] = This is a test $splitArray[4] = ,34.5,17.9, $splitArray[5] = Marc, Cindy and Rob
Now, notice that all of the even subscripts contain the entries that were not in quotes, while the odd subscripts contain the quoted strings. Therefore...
use strict; my $row = "1, \"Hugo, Inc.\", 4, \"This is a test\", 34.5, 17.9, \"Mar +c, Cindy and Rob\""; print "ROW: $row\n"; my @splitArray = split '\"', $row; my @answerArray; for ( my $i = 0 ; $i <= $#splitArray ; $i++ ) { my @tempArray; if ( $i % 2 == 0 ) # if $i is even { @tempArray = split ',', $splitArray[$i]; } else # if $i is odd { @tempArray = ( $splitArray[$i] ); } # Shove all of @tempArray into @answerArray while ( @tempArray ) { if ( $i % 2 == 0 ) { $tempArray[0] =~ s/ //g; } if ( $tempArray[0] =~ /\S/ ) { push @answerArray, shift @tempArray; } else { shift @tempArray; } } } foreach my $answer ( @answerArray ) { print "$answer\n"; }
There is some extra little cleanup garbage in there which you might not want, but that has the basic idea. Hope it helps.
Some people drink from the fountain of knowledge, others just gargle.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: split problems
by michellem (Friar) on Apr 13, 2001 at 01:16 UTC |