substition in perl

D

dennishancy

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? The
documention goes on to say that if no string is specified, the $_
variable is search.

What is the $_ variable?


Dennis Hancy
Eaton Corporation
Cleveland, OH
 
P

Paul Lalli

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
 
B

Bart Van der Donck

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? The
documention goes on to say that if no string is specified, the $_
variable is search.

PATTERN is searched and replaced by REPLACEMENT.

Example:
$s = 'ABC'; # assign value ABC to variable $s
$s =~ s/B/D/; # replace B into D in variable $s
print $s; # print the result (ADC)
What is the $_ variable?

This is the default variable. "where no parameter name is given for"

Example:
for (1..5) {
print $_;
}
# this prints 1 2 3 4 5 (each time the loop is being passed)

It would be the same as
for $number (1..5) {
print $number;
}

See http://learn.perl.org/
 
P

Paul Lalli

Bart said:
PATTERN is searched and replaced by REPLACEMENT.

No. PATTERN is what's being searched for. $_ (or whatever variable is
bound do the s///) is what's being searched. Very different.

Paul Lalli
 
B

Bart Van der Donck

Paul said:
No. PATTERN is what's being searched for. $_ (or whatever variable is
bound do the s///) is what's being searched. Very different.

I see. Then my use of the English verb 'search' was not entirely
correct.

I'm searching a good book.
(=I am looking for something in a book)

I'm searching for a good book.
(=I am looking for a good book)

I thought 'search' and 'search for' could be equivalent in some
situations, depending on context.
 
T

Tad McClellan

Bart Van der Donck said:
I see. Then my use of the English verb 'search' was not entirely
correct.

I'm searching a good book.
(=I am looking for something in a book)


A native speaker would probably have said:

I'm searching in a good book.

I'm searching for a good book.


.... to distinguish their intent from that meaning.

I thought 'search' and 'search for' could be equivalent in some
situations, depending on context.


The unqualified "search" is just plain ambiguous, as there are
always two (or more) entities in a "search", the thing you
are trying to find, and the places where you are looking for it.


So, hauling this thread back on topic:

When a pattern match is not behaving as you expect, there are *two*
things to consider:

the pattern

the string that the pattern is to be matched against
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top