On Tue Mar 31 2009 @ 3:32, Richard Hobson wrote:
> It works, but is there a way of combining these lines:
>
> my $piece = $ref->[$_];
> $piece =~ /.*(..$)/;
>
> It feels like this could be done in one step. Is this correct? I'm
> finding that I'm doing alright in Perl, but I sense the Perl urge to do
> things in as few a number of steps as possible.
You can capture and assign in a list context. (A scalar context won't work,
since it only returns a true or false value then to tell you if you
captured anything at all). This should work:
my ($piece) = ($ref->[$_] = ~ /.*(..$)/);
# Check here for $piece? Ie, check if anything was captured?
# Later
print $pieces{$piece};
See the 'Extracting matches' section of perldoc perlretut.
However, I would recommend that you _avoid_ the urge to do things in as
few steps as possible if it's not clear to you what the short version is
doing.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/