String substitution allows you to replace a pattern or character range with another one using the s/// and tr/// functions.
s/// has two parts: the regular expression and the string to replace it with: s/expression/replacement/.
$h = "Who's afraid of the big bad wolf?"; $i = "He had a wife."; $h =~ s/w.+f/goat/; # yields "Who's afraid of the big bad goat?" $i =~ s/w.+f/goat/; # yields "He had a goate." |
If you extract pattern matches, you can use them in the replacement part of the substitution:
$h = "Who's afraid of the big bad wolf?"; $h =~ s/(\w+) (\w+) wolf/$2 $1 wolf/; # yields "Who's afraid of the bad big wolf?" |
If you don't bind a variable with =~, then s/// operates on $_ just as the match does.
Yes you can:
$h = "Who's afraid of the big bad wolf?"; $animal = 'hyena'; $h =~ s/(\w+) (\w+) wolf/$2 $1 $animal/; # yields "Who's afraid of the bad big hyena?" |
The s/// function can use alternative delimiters, including parentheses and bracket pairs. For example:
$h = "Who's afraid of the big bad wolf?";
$h =~ s!(\w+) (\w+) wolf!$2 $1 wolf!; # using ! as delimiter
$h =~ s{(\w+) (\w+) wolf}{$2 $1 wolf}; # using {} as delimiter
|
The tr/// function allows you to translate one set of characters into another. Specify the source set in the first part of the function, and the destination set in the second part:
$h = "Who's afraid of the big bad wolf?"; $h =~ tr/ao/AO/; # yields "WhO's AfrAid Of the big bAd wOlf?"; |
Like s///, the tr/// function operates on $_ if not otherwise specified.
tr/// returns the number of characters transformed, which is sometimes handy for counting the number of a particular character without actually changing the string.
This example counts N's in a series of DNA sequences:
Code:
while (<>) {
chomp; # assume one sequence per line
my $count = tr/Nn/Nn/;
print "Sequence $_ contains $count Ns\n";
} |
Output:
(~) 50% count_Ns.pl sequence_list.txt Sequence 1 contains 0 Ns Sequence 2 contains 3 Ns Sequence 3 contains 1 Ns Sequence 4 contains 0 Ns ... |
|
| Contents |
Next |