[Israel.pm] CSV

Georges EL OJAIMI g-ojaimi at cyberia.net.lb
Wed Feb 25 02:14:32 PST 2004


Hello Offer,

Thank you very much, your code did it exactly the way I need.
By the way, I knew that PERL will process it very quickly, but your code is
amazing!!! Less than a second . . .
Thanks also for Eitan :)

Best Regards,
Georges

> open(IN,"input.csv") or die "Couldn't open input.csv for reading: $!\n";
> my @csv_input = <IN>;
> close(IN);
> my $lcount = 0;
> while ($lcount <= $#csv_input){
>    if ($csv_input[$lcount] =~ m/^category,class,service,month,year/) {
>       my @in_block;
>       push @in_block,$csv_input[$lcount];
>       while ((($lcount+1) <= $#csv_input) &&
>         ($csv_input[$lcount+1] !~ m/^\s*$/) &&
>         ($csv_input[$lcount+1] !~ m/^category,class,service,month,year/) ) {
>          ++$lcount;
>          push @in_block,$csv_input[$lcount];
>       }
>       chomp @in_block;
>       print $in_block[0],$in_block[2],"\n\n";
>       for my $n (3..$#in_block) {
>          print $in_block[1],$in_block[$n],"\n";
>       }
>       print "\n";
>       next;
>    }
>    ++$lcount;
> }
>
> The code is simplistic because it assumes exactly the input format you
> specified. Note also that it prints to standard output.
> The code does assume that input blocks can be either seperated by empty
> lines or not seperated at all (the conditions for the inner while). If
> anything else is not clear just ask :)
> Regards,






More information about the Perl mailing list