G
gabkin
I am having a problem with the split function.
Here is the sub that it is used in, it should illustrate what I'm
doing, criticism is welcomed...
<PERL SUB>
sub parseLine()
{
#this parses a line which will be in a similar format to this
#"0010230" "Book of the Dead" "Yendor books"
#(tab delimited, escaped by quotes)
#it will take as an argument the column headers and the string to
parse
#it will return a hash,using the columnheader as the key
#and the column data as the element
my $ParseMe = $_[0];
my @ColumnHeaders = $_[1..@_];
my %returnData;
chop($ParseMe);
my @Columns = split(/\t/,$ParseMe);
#my $size=@Columns;print("Size = ",$size,"\n");
for(my $i=0;$i<@Columns;$i++) {
$Columns[$i] =~ s/\"//g; # remove extraneous quotes
#print($ColumnHeaders[$i],"\t",$Columns[$i],"\n");
$returnData{$ColumnHeaders[$i]} = $Columns[$i];
}
return %returnData;
}
</PERL SUB>
(Sorry about the awful two-space indentation, but google seems to strip
out tabs)
A problem has arisen in that in one example, the last four columns are
blank (i.e. null) they're there, theres just nothing in them. For these
last four, the split function seems to discard them. I checked this
with the aid of the commented out lines.
Is there a way to force split to not lose the blank columns?
Or would I have to 're-invent' the split algorithm so as to keep them?
Any help would be greatly appreciated...
Here is the sub that it is used in, it should illustrate what I'm
doing, criticism is welcomed...
<PERL SUB>
sub parseLine()
{
#this parses a line which will be in a similar format to this
#"0010230" "Book of the Dead" "Yendor books"
#(tab delimited, escaped by quotes)
#it will take as an argument the column headers and the string to
parse
#it will return a hash,using the columnheader as the key
#and the column data as the element
my $ParseMe = $_[0];
my @ColumnHeaders = $_[1..@_];
my %returnData;
chop($ParseMe);
my @Columns = split(/\t/,$ParseMe);
#my $size=@Columns;print("Size = ",$size,"\n");
for(my $i=0;$i<@Columns;$i++) {
$Columns[$i] =~ s/\"//g; # remove extraneous quotes
#print($ColumnHeaders[$i],"\t",$Columns[$i],"\n");
$returnData{$ColumnHeaders[$i]} = $Columns[$i];
}
return %returnData;
}
</PERL SUB>
(Sorry about the awful two-space indentation, but google seems to strip
out tabs)
A problem has arisen in that in one example, the last four columns are
blank (i.e. null) they're there, theres just nothing in them. For these
last four, the split function seems to discard them. I checked this
with the aid of the commented out lines.
Is there a way to force split to not lose the blank columns?
Or would I have to 're-invent' the split algorithm so as to keep them?
Any help would be greatly appreciated...