Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^12: Mapping & Hash Issues

by sandeepsinghperl (Novice)
on Mar 29, 2017 at 21:27 UTC ( [id://1186438]=note: print w/replies, xml ) Need Help??


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

Yes, i did.

Replies are listed 'Best First'.
Re^13: Mapping & Hash Issues
by sandeepsinghperl (Novice) on Mar 29, 2017 at 22:35 UTC

    I change my code little differently and now its working, Please find 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; ($OLDCostCenter,$OLDActivity,$NEWCostCenter,$NEWActivity,$OLDAcct,$NEW +Acct) = split(',',$_); + if( "$OLDCostCenter" eq "$CostCenter" and "$OLDActi +vity" eq "$Activity") { $c1 = "$NEWCostCenter"; + $a1 = "$NEWActivity"; goto ed; } else { $c1 = "$CostCenter"; $a1 = + "$Activity"; + } } 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);

      You don't need to read the MAP file for every line of the input, just read it once

      #!/usr/bin/perl use strict; open MAPFile,'<',$ARGV[2] or die "$ARGV[2] : $!"; my %hash = (); # Cost Center, Activity my %hash1 = (); # Account while ( <MAPFile> ) { chomp; my ($CC,$Act,$NEWCC,$NEWAct,$Acct,$NEWAcct) = split(',',$_); $hash{$CC}{$Act}[0] = $NEWCC; $hash{$CC}{$Act}[1] = $NEWAct; $hash1{$Acct} = $NEWAcct; } close MAPFile; open SourceFile, '<', $ARGV[0] or die "$ARGV[0] : $!"; open TargetFile, '>', $ARGV[1] or die "$ARGV[1] : $!"; my $count = 0; my $changed = 0; my $changed1 = 0; while (<SourceFile>) { ++$count; my ($Year,$HSP_rates,$Curr,$Scenario,$Version,$Product, $CostCenter,$Activity,$Entity,$Acct,$BegBal,@mth) = split(',',$_); my ($NewCC,$NewAct,$NewAcct); if ( exists $hash{$CostCenter}{$Activity} ){ $NewCC = $hash{$CostCenter}{$Activity}[0]; $NewAct = $hash{$CostCenter}{$Activity}[1]; print "line $count : updated $CostCenter to $NewCC\n"; ++$changed; } else { $NewCC = $CostCenter; $NewAct = $Activity; } if ( exists $hash1{$Acct} ){ $NewAcct = $hash1{$Acct}; print "line $count : updated $Acct to $NewAcct\n"; ++$changed1; } else { $NewAcct = $Acct; } print TargetFile join ',', $Year,$HSP_rates,$Curr,$Scenario,$Version,$Product, $NewCC,$NewAct,$Entity,$NewAcct,$BegBal,@mth; } print "$count lines processed $changed changes to CostCenter,Activity codes $changed1 changes to Account codes\n"; close SourceFile; close TargetFile;

      Update ; %hash1 added for Acct mappings


      poj
        Thats the smart way. Thank you so much for helping me in troubled times.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-19 03:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found