Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

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

Try this code to see if it is what you are looking for.

#!/usr/bin/perl -w my $data = 'reference.txt'; open INFILE, '<', $data; my %hash = (); while (<INFILE>) { chomp($_); my ($id,$description)=split(/\t/); #print $id,' << ',$description,"\n"; $hash{ $id } = $description; } #print "\n"; #while ( my ($key, $value) = each(%hash) ) { # print "$key => $value\n"; # } my $data2 = "readthisfile.txt"; open INFILE2, "<", $data2; while(<INFILE2>) { chomp($_); @parts = split /\t/; print "Original \t",++$i,': ',$_,"\n"; print "Replaced \t",$i,': '; foreach $parl(@parts){ if ($parl =~ m/(.*\{)([0-9]+)(\}.*)/) { $parl = $1.$hash{$2}.$3 if exists $hash{$2}; } print $parl,"\t"; } print "\n"; }
perl "D:\Temp\repalce1.pl" Process started >>> Original 1: ABCDE Replaced 1: ABCDE Original 2: + + Replaced 2: Original 3: +P237S S{301609239} + + + Replaced 3: +P237S S{kitchen} Original 4: +R372W W{114672573} + + + Replaced 4: +R372W W{study room} Original 5: +K576R R{20531742} R{89242146} + + + Replaced 5: +K576R R{living room} R{terrace} Original 6: +R1059Q Q{126723431} Q{6934272} Replaced 6: +R1059Q Q{126723431} Q{6934272} Original 7: Replaced 7: Original 8: STRKE Replaced 8: STRKE Original 9: Replaced 9: Original 10: +T216I I{301781720} I{148237434} + + + Replaced 10: +T216I I{balcony} I{148237434} Original 11: +V275I I{149632297} I{47224534} + + + Replaced 11: +V275I I{149632297} I{47224534} Original 12: +R13H H{126333615} + + + Replaced 12: +R13H H{126333615} Original 13: +F113L L{301781720} L{148237434} + + + Replaced 13: +F113L L{balcony} L{148237434} Original 14: +G135S S{147902132} S{47224534} S{125864042} + S{107921834} + + Replaced 14: +G135S S{147902132} S{47224534} S{125864042} + S{107921834} Original 15: +T307A A{224050516} A{126333615} + + + Replaced 15: +T307A A{224050516} A{126333615} Original 16: +L217F F{149632297} F{147902132} Replaced 16: +L217F F{149632297} F{147902132} <<< Process finished. ================ READY ================

In reply to Re: Search & Replace Across Two Files by Generoso
in thread Search & Replace Across Two Files by sophix

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 learning in the Monastery: (4)
As of 2024-04-26 01:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found