Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Mapping & Hash Issues by sandeepsinghperl

Title:
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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found