http://www.perlmonks.org?node_id=999666

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;

Replies are listed 'Best First'.
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 (Cardinal) 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.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      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?
      You provided an excellent solution there, choroba.