in reply to Mapping & Hash Issues

a) Use <code/> tags to make your post readable.
b) Put use strict; and use warnings; at the top of your program, always!
c) Use Text::CSV to read and write "comma separated values" files that might have quotes around the values.
d) Read MappingFile once at the beginning of your program, don't keep opening it and re-reading it in the main loop.

Replies are listed 'Best First'.
Re^2: Mapping & Hash Issues
by sandeepsinghperl (Novice) on Mar 29, 2017 at 17:12 UTC
    Hi, I am very new to perl and struggling. Is it possible please if you can give me the code as per the requirement. Somehow <code> is not working for me sure i am doing something wrong. Thanks
      use strict; use warnings; use Text::CSV; die "usage: $0 SourceFile TargetFile MappingFile\n" if @ARGV < 3; my $csv = Text::CSV->new({ auto_diag=>1, binary=>1, eol=>"\n" }); my %map; open my $MAP, '<', $ARGV[2] or die "Can't read $ARGV[2]: $!"; while (my $row = $csv->getline($MAP)) { my ($old_cost, $old_act, $new_cost, $new_act) = @$row; $map{$old_cost}{$old_act} = [$new_cost, $new_act]; } close $MAP; open my $IN, '<', $ARGV[0] or die "Can't read $ARGV[0]: $!"; open my $OUT, '>', $ARGV[1] or die "Can't write $ARGV[1]: $!"; while (my $row = $csv->getline($IN)) { if (my $new = $map{$row->[6]}{$row->[7]}) { $row->[6] = $new->[0]; $row->[7] = $new->[1]; } $csv->print($OUT, $row); } close $IN; close $OUT;
        Re: Give a man a fish...
        Sometimes, you have to show him what a fish looks like first.

        First , thanks for being patient and youyr help. i ran the code its giving the below Error and nothing is coming up in the Target file.

        =========================================================================================

        C:\Users\sksingh>perl "C:\Users\sksingh\Documents\Barrick Work\Perl\GC +OA\newway.pl" "C:\Users\sksingh\Documents\Barrick Work\Perl\GCOA\NAAC +T.txt" "C:\Users\sksingh\Documents\Barrick Work\Perl\GCOA\NAACTDEPEND +.txt" "C:\Users\sksingh\Documents\Barrick Work\Perl\GCOA\FINALMAPDEPE +NDENT.txt" # CSV_XS ERROR: 2023 - EIQ - QUO character not allowed @ rec 1840 pos +12 field 1

        =========================================================================================