http://qs321.pair.com?node_id=494794


in reply to Parsing and modifying CSV files

Step 1: get all your stock data into a hash as suggested by poj

use strict; use Data::Dumper; my %stock; while (<DATA>) { my ($qty,$model,$size) = split(',',$_); $stock{$model} = [0,0,0,0] unless (exists $stock{$model}); $stock{$model}[0] = $qty if ($size =~ /sm/i); $stock{$model}[1] = $qty if ($size =~ /md/i); $stock{$model}[2] = $qty if ($size =~ /lg/i); $stock{$model}[3] = $qty if ($size =~ /xl/i); } print Dumper(\%stock); __DATA__ 224,128,MD,B,840197082997 0,128,LG,B,840197083000 0,128,XL,B,840197083017 0,12,SM,B,840197082997 15,12,LG,B,840197083000 0,12,XL,B,840197083017 32,8,SM,B,840197082997

Step 2: Update file2 data

use strict; ## simulate stock data from above script 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; $data[9] = $sm unless ($sm); $data[12] = $md unless ($md); $data[15] = $lg unless ($lg); $data[18] = $xl unless ($xl); } print "@data\n"; } # I've assumed you want the 'price' element (??) left alone unless 'qt +y' is zero # in which case set it to zero. __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

Replies are listed 'Best First'.
Re^2: Parsing and modifying CSV files
by ch1 (Novice) on Sep 25, 2005 at 07:54 UTC
    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.
      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.