QM has asked for the wisdom of the Perl Monks concerning the following question:
Being in a contrary mood, I wondered if I could do that in one statement, and tried this:my $x = 'abc12345'; my ($y) = $x =~ /(\d+)$/; my $length = length($y);
I came up with this, but it wasn't satisfying:my $x = 'abc12345'; my $length = length ($x =~ /(\d+)$/); # returns 1, the length of the number in the number of elements
(Yes, useles use of map might deserve its own warning.)my $x = 'abc12345'; my $length = map { length } ($x =~ /(\d+)$/);
To generalize for cases where there might be more matches (a variable in the regex, for example), I generalized to this:
But that led me to this:my $x = 'abc12345'; my $length = map { length } ($x =~ /(\d+)$/)[0];
Is there a better way to do this, or have I hit the practical limit?my $x = 'abc12345'; my $length = length (($x =~ /(\d+)$/)[0]); # returns 5
-QM
--
Quantum Mechanics: The dreams stuff is made of
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Length of first captured match
by Tanktalus (Canon) on May 05, 2006 at 23:58 UTC | |
by QM (Parson) on May 07, 2006 at 01:21 UTC | |
|
Re: Length of first captured match
by GrandFather (Saint) on May 06, 2006 at 00:01 UTC | |
|
Re: Length of first captured match
by japhy (Canon) on May 06, 2006 at 03:56 UTC | |
|
Re: Length of first captured match
by rhesa (Vicar) on May 06, 2006 at 00:04 UTC | |
|
Re: Length of first captured match
by ikegami (Patriarch) on May 06, 2006 at 00:51 UTC | |
|
Re: Length of first captured match
by NetWallah (Canon) on May 06, 2006 at 06:32 UTC | |
by ikegami (Patriarch) on May 07, 2006 at 01:27 UTC | |
|
Re: Length of first captured match
by NetWallah (Canon) on May 06, 2006 at 00:36 UTC | |
by Limbic~Region (Chancellor) on May 06, 2006 at 14:34 UTC |