in reply to Re^2: Mapping & Hash Issues
in thread Mapping & Hash Issues

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;

Replies are listed 'Best First'.
Re^4: Mapping & Hash Issues
by Anonymous Monk on Mar 29, 2017 at 17:16 UTC
    Re: Give a man a fish...
    Sometimes, you have to show him what a fish looks like first.
Re^4: Mapping & Hash Issues
by sandeepsinghperl (Novice) on Mar 29, 2017 at 17:29 UTC

    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\" "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


      First, <code> tags are easy to use. When you're entering your post, type something like this:
      for my $i (1 .. 10) { print "And a $_,\n"; }

      And it will come out looking nice. If you leave off the <code> and </code>, it will be all jumbled together and people will yell at you. Also, instead of typing a row of equal signs like you've been doing, you can instead use <hr>, which stands for horizontal rule.

      Second, as runrig says, there's probably something weird in your input file. Try looking at line 1840. Try making a smaller input file to make testing easier.

        The record number (1840) is inaccurate because it's counting the map file as well. You might want to put this line just above the start of the second while loop:
        $csv = Text::CSV->new({ auto_diag=>1, binary=>1, eol=>"\n" });
        (Yes, that's almost a copy of a previous line, but note that the my is missing.)
      Maybe there's something wrong with record 1840 in your input file?


        My Input file is very small- 3 lines

        I updated the script by removing my. But it just updated the first row with correct Cocenter & Activity but Second Row it just updated the Costcenter but not the Activity. See below out file - first row is fine but second it should also update the Activity to A5854.

        "BegBalance" "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oc +t" "Nov" "Dec" "Period" ,,,,,,,,,,,,,,,,,, "FY01","HSP_InputValue","Local","Plan","Final","10","C4910","A5854","E +1900","52112",#Mi,1,2,3,4,5,6,7,8,9,10,11,12 "FY01","HSP_InputValue","Local","Plan","Final","10","C4910","A0000","E +1900","52122",#Mi,1,2,3,4,5,6,7,8,9,10,11,12