in reply to Replace many array element with another corresponding array element in a file
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Replace many array element with another corresponding array element in a file
by faezshingeri (Initiate) on Oct 18, 2012 at 05:31 UTC | |
by Kenosis (Priest) on Oct 18, 2012 at 05:39 UTC |