Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re^14: Mapping & Hash Issues by poj
in thread Mapping & Hash Issues by sandeepsinghperl

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2023-02-07 14:29 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (40 votes). Check out past polls.