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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: The return value of m// (clues)
by tye (Sage) on Apr 23, 2012 at 04:46 UTC | |
by sophate (Beadle) on Apr 23, 2012 at 05:25 UTC | |
by kcott (Archbishop) on Apr 23, 2012 at 07:56 UTC | |
by sophate (Beadle) on Apr 23, 2012 at 09:41 UTC | |
by quester (Vicar) on Apr 23, 2012 at 07:04 UTC | |
by bimleshsharma (Beadle) on Apr 23, 2012 at 07:47 UTC | |
|
Re: The return value of m//
by 2teez (Vicar) on Apr 23, 2012 at 08:47 UTC | |
by sophate (Beadle) on Apr 23, 2012 at 09:43 UTC | |
|
Re: The return value of m//
by Anonymous Monk on Apr 23, 2012 at 15:28 UTC |