Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^14: Mapping & Hash Issues

by poj (Abbot)
on Mar 30, 2017 at 07:36 UTC ( #1186464=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^15: Mapping & Hash Issues
by sandeepsinghperl (Novice) on Mar 30, 2017 at 15:53 UTC
    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://1186464]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2022-12-01 01:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?