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 | |
by sandeepsinghperl (Novice) on Mar 29, 2017 at 17:12 UTC | |
by Anonymous Monk on Mar 29, 2017 at 17:14 UTC | |
by Anonymous Monk on Mar 29, 2017 at 17:16 UTC | |
by sandeepsinghperl (Novice) on Mar 29, 2017 at 17:29 UTC | |
by Anonymous Monk on Mar 29, 2017 at 17:51 UTC | |
| |
by runrig (Abbot) on Mar 29, 2017 at 17:34 UTC | |
|
Back to
Seekers of Perl Wisdom