Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

The return value of m//

by sophate (Beadle)
on Apr 23, 2012 at 04:30 UTC ( #966519=perlquestion: print w/ replies, xml ) Need Help??
sophate has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

A quick question about the return value of //. I have a simple script as shown below. I expect the 2nd and 3rd print statements print "5" but the 2nd print actually prints "4" instead. Really appreciate that if any one can explain why the 2nd print statement prints "4". Thanks.

#!/usr/bin/perl use strict; use warnings; my $a=" 456 789 123 456 789 "; my $count; $count = $a =~ /\d+/g ; print "$count\n"; $count = () = $a =~ /\d+/g ; print "$count\n"; my @a = $a =~ /\d+/g ; $count = @a; print "$count\n";

Results:

1 4 5

Comment on The return value of m//
Select or Download Code
Re: The return value of m// (clues)
by tye (Cardinal) on Apr 23, 2012 at 04:46 UTC

    Swap the positions in the file of the last two cases to get one clue. Once you've done that, print out what gets put into @a for another clue.

    - tye        

      Really interesting! I changed the script to:

      #!/usr/bin/perl use strict; use warnings; my $a=" 456 789 123 456 789 "; my $count; $count = $a =~ /\d+/g ; print "$count\n"; my @a = $a =~ /\d+/g ; $count = @a; print "@a\n"; print "$count\n"; $count = () = $a =~ /\d+/g ; print "$count\n";

      The results are:<\p>

      1 789 123 456 789 4 5

      Could you please advise why the 1st digit group is gone the first time i call $a =~ /\d+/g ?

        Hint: look for the phrase "search position" in perlop. Also consider that scalar global matches are typically used in loops, for example

        for my $match ($a =~ /\d+/g) { print "$match\n" }

        Really i could not understand but the code

        $count = $a =~ /\d+/g ; vs. $count = () = $a =~ /\d+/g ;

        is an issue.

        When you use the g modifier, it keeps track of its last match. In the first RE (scalar context), 456 is matched: your count indicates a single match (see code below for actual value). In the second RE (list context), the remaining four sets of digits are matched. You can use pos to reset the last match position.

        $ perl -Mstrict -Mwarnings -e ' > my $a=" 456 789 123 456 789 "; > my $count; > $count = $a =~ /\d+/g ; > print "$count\n"; > print "${^MATCH}\n"; > pos($a) = 0; > my @a = $a =~ /\d+/g ; > $count = @a; > print "@a\n"; > print "$count\n"; > ' 1 456 456 789 123 456 789 5

        ${^MATCH} is described in perlvar.

        -- Ken

Re: The return value of m//
by 2teez (Priest) on Apr 23, 2012 at 08:47 UTC

    The second print statement print '4', because you have used /g in your first statement in a scalar context, so the position of the digits to count is less than one, thus the value '4' was printed. Please, note that as long as your match didn't fail, your position changes

    check this:

    #!/usr/bin/perl use strict; use warnings; my $a=" 456 789 123 456 789 "; my $count; $count=$a =~ /\d+/g; print "$count\n"; # output '1', position changes $count=$a =~ /\d+/g; print "$count\n"; # output '1' position changes $count = () = $a =~ /\d+/g; print "$count\n"; # output '3' my @a = $a =~ /\d+/g; $count = @a; print "$count\n";

    if you wanted to get '5', at the second print (a) remove the /g, then the second print gives you a '5', like this:

    #!/usr/bin/perl use strict; use warnings; my $a=" 456 789 123 456 789 "; my $count; $count=$a =~ /\d+/; print "$count\n"; $count = () = $a =~ /\d+/g; print "$count\n"; my @a = $a =~ /\d+/g; $count = @a; print "$count\n";

    (b) In another case, you can do this:

    #!/usr/bin/perl use strict; use warnings; my $a=" 456 789 123 456 789 "; my $count; my $b=$a; $count=$a =~ /\d+/g; print "$count\n"; $count = () = $b =~ /\d+/g; print "$count\n"; my @a = $b =~ /\d+/g; $count = @a; print "$count\n";
    Hope this helps.

      Thanks you for you detailed explanation :-)

Re: The return value of m//
by Anonymous Monk on Apr 23, 2012 at 15:28 UTC
    ++$count while $a =~ /\d+/g;
    or $count = $a =~ s/\d+\K//g;

    By the way, DON'T use $a as a variable! It is used for sort.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://966519]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-12-28 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (180 votes), past polls