Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Replace many array element with another corresponding array element in a file

by faezshingeri (Initiate)
on Oct 18, 2012 at 04:10 UTC ( #999666=perlquestion: print w/ replies, xml ) Need Help??
faezshingeri has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to replace many array element with another corresponding array element in a file, but its taking ages to execute. Is there a simpler approach ? Below is my code:

open( my $in, '<', "Test.txt") or die "cannot open Test.txt $!"; open( my $out, '>', "TestFinal.txt") or die "cannot create TestFinal $ +!"; while( <$in>) { for(my $i=2 ; $i<=$LastRowGlossary; $i++) { s/$variable[$i]/$vardescription[$i]/g; } for(my $j=2 ; $j<=$LastRowTable; $j++) { s/$COVERAGE_TYPE_CODE[$j]/$TCOVERAGE[$j]/g; s/$CVG_TEST_CRIT_CD[$j]/$TCVG_TEST_CRIT_TYP[$j]/g; } print {$out} $_; } close $in; close $out;

Comment on Replace many array element with another corresponding array element in a file
Download Code
Re: Replace many array element with another corresponding array element in a file
by Kenosis (Priest) on Oct 18, 2012 at 05:18 UTC

    If your substitutions are working fine (no word-boundary issues), but just taking a long time (understandable, iterating through two arrays for substitutions on each file line), perhaps the following will be helpful:

    my $TestText; { local $/; open( my $in, '<', "Test.txt" ) or die "cannot open Test.txt $!"; $TestText = <$in>; close $in; } $TestText =~ s/\Q$_\E/$hash{$_}/g for keys %hash; open( my $out, '>', "TestFinal.txt" ) or die "cannot create TestFinal +$!"; print $out $TestText; close $out;

    This reads the entire file into a scalar, so substitutions are done on the entire file's contents. This also uses a hash's key/value pairs instead of different array element pairs, so you'll need to initialize the hash (%hash) accordingly for the three substitution pairs.

    Hope this helps!

    Edit: my $TestText ... to $TestText ... in the file slurp block and quotemeta \Q$_\E.

      I tried the following:
      my %hashvariable; @hashvariable{@variable} = @vardescription; my $TestText; { local $/; open( my $in, '<', "Test.txt" ) or die "cannot open Test.txt $!"; my $TestText = <$in>; close $in; } $TestText =~ s/$_/$hashvariable{$_}/g for keys %hashvariable; open( my $out, '>', "TestFinal.txt" ) or die "cannot create TestFinal +$!"; print $out $TestText; close $out;
      Output file TestFinal.txt is empty. Am I doing anything wrong?

        Oh, sheesh! No, I did something wrong.:

        my $TestText = <$in>;

        should be:

        $TestText = <$in>;

        Sorry about that. Will correct this in the original posting

        BTW - Excellent way to initialize the hash...

Re: Replace many array element with another corresponding array element in a file
by choroba (Abbot) on Oct 18, 2012 at 05:55 UTC
    Crossposted at StackOverflow. It is considered polite to inform about crossposting so people not attending both sites do not waste their efforts solving a problem already solved at the other end of the internet.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      You provided an excellent solution there, choroba.
      I am also unable to metaquote the array elements as I am getting them from excel sheet.
      $variable[$r] = $Sheet->Cells($r,1)->{'Value'}; $vardescription[$r]=$Sheet->Cells($r,2)->{'Value'};
      How do I use the qw option as you have done?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999666]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (14)
As of 2014-07-28 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (201 votes), past polls