Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Mapping & Hash Issues

by sandeepsinghperl (Novice)
on Mar 29, 2017 at 16:21 UTC ( #1186396=perlquestion: print w/replies, xml ) Need Help??

sandeepsinghperl has asked for the wisdom of the Perl Monks concerning the following question:

I am struggling with the Perl Hash conversion.

I have a source file where i would like to replace Cost center(C4000) & Activity (A0000) simultaneously after checking the same in a mapping file with new values. My requirement is also that it should read line by line and replace the both Costcenter and Activity for all possible intersections.

Sourcefile

"FY01","HSP","Local","Plan","Final","300","C4000","A0000","Entity","52 +112",#Mi,1,2,3,4,5,6,7,8,9,10,11,12 "FY01","HSP_","Local","Plan","Final","300","C4000","A0000","Entity","5 +2122",#Mi,1,2,3,4,5,6,7,8,9,10,11,12

MappingFile

"C4000","A0000",C4800,A1900

Resultant/Target file should be

"FY17","HSP_InputValue","Local","Plan","Final","10","C4800","A1900","E +ntity","52112",#Mi,1,2,3,4,5,6,7,8,9,10,11,12 "FY17","HSP_InputValue","Local","Plan","Final","10","C4800","A1900","E +ntity","52122",#Mi,1,2,3,4,5,6,7,8,9,10,11,12

My Perl Script is below.

open (SourceFile, "$ARGV[0]"); open (TargetFile, ">$ARGV[1]"); while (<SourceFile>) { # Remove the last character from the line. #$Line = substr($_,0,-1); chomp; ($Year,$HSP_rates,$Curr,$Scenario,$Version,$Product,$CostCenter,$Activ +ity,$Entity,$Acct,$BegBal,$Jan,$Feb,$Mar,$Apr,$May,$Jun,$Jul,$Aug,$Se +p,$Oct,$Nov,$Dec) = split(',',$_); print TargetFile "$Year,"; print TargetFile "$HSP_rates,"; print TargetFile "$Curr,"; print TargetFile "$Scenario,"; print TargetFile "$Version,"; print TargetFile "$Product,"; if( length( $CostCenter ) > 0 ) { open (MAPFile,"$ARGV[2]"); while ( <MAPFile> ) { chomp; my @line = split(',', $_); $hash{$line[0]} = $line[0]; $hash1{$line[0]} = $line[2]; $hash2{$line[1]} = $line[1]; $hash3{$line[1 +]} = $line[3]; if( $hash{"$CostCenter"} eq "$CostCenter" and $hash +2{"$Activity"} eq "$Activity") { $c1 = $hash1{"$CostCenter"} a +nd + $a1 = $hash3{"$Activity"}; goto ed; } else { $c1 = "$CostCenter"; $a1 = + "$Curr"; + } } ed: close(MAPFile); print TargetFile "$c1,"; print TargetFile "$a1,"; print TargetFile "$Entity,"; } if( length( $Acct ) > 0 ) { open (MAPFile,"$ARGV[2]"); while ( <MAPFile> ) { chomp; my @line = split(","); $hash{$line[4]} = $line[4]; $hash1{$line[4]} = $line[5]; $hash2{$line[4]} = $line[5]; if( $hash{"$Acct"} eq "$Acct" ) { $b1 = $hash1{"$Acct"}; goto ed; } else { $b1 = "$Acct"; } } ed: close(MAPFile); print TargetFile "$b1,"; } print TargetFile "$BegBal,"; print TargetFile "$Jan,"; print TargetFile "$Feb,"; print TargetFile "$Mar,"; print TargetFile "$Apr,"; print TargetFile "$May,"; print TargetFile "$Jun,"; print TargetFile "$Jul,"; print TargetFile "$Aug,"; print TargetFile "$Sep,"; print TargetFile "$Oct,"; print TargetFile "$Nov,"; print TargetFile "$Dec"; print TargetFile "\n"; } close (SourceFile); close (TargetFile);

Please help.

Replies are listed 'Best First'.
Re: Mapping & Hash Issues
by toolic (Bishop) on Mar 29, 2017 at 16:43 UTC
Re: Mapping & Hash Issues
by Anonymous Monk on Mar 29, 2017 at 16:45 UTC
    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.
      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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1186396]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-10-06 23:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (29 votes). Check out past polls.

    Notices?