in reply to Re: Parsing and modifying CSV files
in thread Parsing and modifying CSV files

Wow..... I was way off.
Here is an update:
#!/usr/local/bin/perl use strict; my %stock; $stock{'128'} = [0,224,0,0]; $stock{'12'} = [0,0,15,0]; $stock{'8'} = [32,0,0,0]; while (<DATA>) { ## file2 my @data = split(',',$_); if (exists $stock{$data[0]}) { my $ary_ref = $stock{$data[0]}; my ($sm,$md,$lg,$xl) = @$ary_ref; if($sm eq '0'){$sm=''}else{$sm=0}; if($md eq '0'){$md=''}else{$md=0}; if($lg eq '0'){$lg=''}else{$lg=0}; if($xl eq '0'){$xl=''}else{$xl=0}; $data[9] = $sm; $data[12] = $md; $data[15] = $lg; $data[18] = $xl; } print (join(',', @data)); } __DATA__ 128,1,Download,0,,TEXT,2,Size,2,??,SM,3,??,MD,4,??,LG,5,??,XL 12,1,Download,0,,TEXT,2,Size,2,??,SM,3,??,MD,4,??,LG,5,??,XL 8,1,Download,0,,TEXT,2,Size,2,??,SM,3,??,MD,4,??,LG,5,??,XL
What's scary is.... I Almost understand it.
Thanks to everyone that replied.

Replies are listed 'Best First'.
Re^3: Parsing and modifying CSV files
by poj (Abbot) on Sep 25, 2005 at 08:56 UTC
    Another way would be to use the conditional operator inside a loop
    # field number for sizes SM MD LG XL my @fno =(9,12,15,18); while (<DATA>) { ## file2 my @data = split(',',$_); my $ky = $data[0]; if (exists $stock{$ky}) { # check each size for my $sz (0..3){ $data[$fno[$sz]] = ( $stock{$ky}[$sz] eq '0' ) ? '' : '0' ; } } print (join(',', @data)); }
    poj
      If you substitute $sz with 0 the expression becomes $data[$fno[0]] and because $fno[0] = 9 this equates to $data[9] If the expression $stock{$ky}[0] eq '0' is true then $data[9] = '' else $data[9] = '0'
      poj
      Can you explain $data[$fno$sz] = ( $stock{$ky}$sz eq '0' ) ? '' : '0' ; ?

      Is it setting the value of the array to 0 or ''? I don't understand the brackets around $sz.