tom smith wrote:
On Tue, Nov 3, 2009 at 4:33 PM, tom smith <climbingpartn...@gmail.com>wrote:

Thanks for the tips!  More comments below.
I saw it written the other way somewhere, and I thought it looked cleaner.
 I'll do it your way from now on.


    if ($line =~ /\((.*?)\)/) {
       $line =~ s/$1/$i;$j/;

If you have two regular expressions that are doing the same thing then you
are probably doing it incorrectly:

    if ( $line =~ s/\((.*?)\)/($i;$j)/ ) {

Ah.  Nice.


Wait a minute, on further review I don't understand how that if condition
works.  According to perlop:

----
s/PATTERN/REPLACEMENT/egimosx
               Searches a string for a pattern, and if found, replaces that
               pattern with the replacement text and returns the number of
               substitutions made.  Otherwise it returns false
(specifically,
               the empty string).

               ....
-----

So if no substitution takes place, s/// returns an empty string.   Then in
the case of no substitution, wouldn't that leave me with:

    if ($line =~ "")

It *returns* an empty string (false), the same way that the match operator returns false (the empty string), it doesn't modify its bound variable to an empty string.


???   And that condition always evaluates to true as far as I can tell.
 Your if statement does work as advertised:

use strict;
use warnings;

$\ = "\n";


my $line = 'hello';

if ($line =~ s/z/e/) {  #no z's in 'hello', so no substitution
    print "matched!";
    print $line;
} else {
    print "no match";
    print $line;
}

--output:--
not match
hello

...but I don't understand why it works.

"$line" is bound to "s/z/e/" through the "=~" binding operator. The expression "$line =~ s/z/e/" returns the number of substitutions (TRUE) or an empty string (FALSE).



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.               -- Damian Conway

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to