Re: substition in perl



dennisha...@xxxxxxxxx wrote:
Trying to figure out the substitution operator in Perl.

The documentation shows the syntax as

s/PATTERN/REPLALCEMENT/

and explains that the operator searches a string for PATTERN and
replaces that match with the REPLACEMENT text.

My question then is this. What string is being searched?

By default, the $_ variable. You can choose a different string,
however, by using the =~ (binding) operator, like so:

my $string = "hello world";
$string =~ s/hello/goodbye/;
print $string; # prints "goodbye world"

The
documention goes on to say that if no string is specified, the $_
variable is search.

What is the $_ variable?

$_ is the "default" variable for many many functions in Perl. It is
used as something of a shortcut, both for assigning to variables and
reading from variables. Some examples:

while (<$fh>) {
chomp;
s/hello/goodbye/;
my @fields = split /\t/;
foreach (@fields) {
my $len = length;
print;
}
}

Each one of these lines uses the $_ variable. The <$fh> (reading from
the $fh filehandle) as the sole condition to a while loop puts the line
it read into $_. chomp() removes the newlines from the end of $_.
s///, as you read above, searches-and-replaces on $_. split() splits
the string stored in $_. foreach puts each element of its list in $_
as it iterates over them. length() returns the length of $_ by
default. And print prints the contents of $_ by default.

We could re-write the above by explitily typing $_ everywhere that it's
implicitly used, and we'd get:

while ($_ = <$fh>) {
chomp $_;
$_ =~ s/hello/goodbye/;
my @fields = split (/\t/, $_);
foreach $_ (@fields) {
my $len = length($_);
print $_;
}
}

`perldoc perlvar` gives a complete list of all the places $_ is used
implicitly, by the way.

Hope this helps,
Paul Lalli

.



Relevant Pages

  • Re: another docs problem - imp
    ... > The first sentence says that the _path_ argument is a search path. ... Correct it does not say it's a string. ... > technical documentation a bit more carefully. ... Yes, read it a second time, didn't see the alternate interpretation. ...
    (comp.lang.python)
  • Re: Reading a line of text from a stream
    ... * Reads a line of text from a stream and returns the string on * ... If maxRead is NULL then SIZE_MAX-1 is * considered ... You've accidentally left out documentation that if maxRead is not NULL, ... It is not possible to distinguish between I/O failures and allocation ...
    (comp.lang.c)
  • Re: empty vs null
    ... The documentation you refer to is for the System.String type, ... though they moved to being immutable reference types. ... to Vb.NET, suddenly we could actually compare string references, and at the same ... > references compare equal to each other. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Reading a line of text from a stream
    ... Reads a line of text from a stream and returns the string on ... If maxRead is NULL then SIZE_MAX-1 is ... The documentation above is also a bit hard to read; ... You've also neglected to explicitly specify that the terminating "\n" ...
    (comp.lang.c)
  • Re: Help with double arithmetic
    ... The call to printwill convert the double to String using: ... The documentation on String.valueOfindicates it is the same as: ... rather than letting println() convert ... DecimalFormat documentation if you want to format it differently. ...
    (comp.lang.java.programmer)