jthiel has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/env perl use strict; use warnings; my $test = 'x x x a x x x b x x x a x x x b x x x'; print "ORIGINAL: $test\n\n"; # 1 - array my @array = split(/ /, $test); for (@array) { if ($_ eq 'a') { $_ = 'b'; } elsif ($_ eq 'b') { $_ = 'a'; } } print "ARRAY: @array\n"; # 2 - s/// (FAILS) my $s = $test; $s =~ s/a/b/g; $s =~ s/b/a/g; print "s///: $s\n"; # 3 - s/// map my $map = join(' ', map{ if (/a/) { s/a/b/; } elsif (/b/) { s/b/a/; } +$_ } split(/ /, $test)); print "s/// MAP: $map\n"; # 4 - substr my $substr = $test; for (my $i = 0; $i <= length($substr); $i++) { if (substr($substr, $i, 1) eq 'a') { substr($substr, $i, 1) = 'b'; +} elsif (substr($substr, $i, 1) eq 'b') { substr($substr, $i, 1) = 'a +'; } } print "SUBSTR: $substr\n";
|
|---|