Am 04.03.2014 00:35, schrieb shawn wilson:
So, when I do this:
my $src = (grep {/.*(ARIN|APNIC).*(\n)?/; $1} @ret)[0];
I get a full line and not just the capture:
# ARIN WHOIS data and services are subject to the Terms of Use
When I do this:
my $src = (grep {s/.*(ARIN|APNIC).*(\n)?/$1/} @ret)[0];
I get just the match but it also alters the array
You can simplify your regexp to just
/(ARIN|APNIC)/;
I assume you intend to get the first match in the array.
In case, another solution that is short, readable and only reads the
array to first occurence would be:
use List::Util qw/first/;
my $src;
first {($src) = /(ARIN|APNIC)/} @ret;
or
use List::Util qw/first/;
my $src;
first {/(ARIN|APNIC)/ and Ssrc = $1} @ret;
Disadavantage:
It uses a side effect in first {...} @... block,
that is not the usual way.
[--
From a higher point, it would be better to use lazy lists,
in that case you could use just write it as
[pseudocode]
$src = first map {match => $1} @ret;
lazy lists are only evaluated when needed, so it would stop at first
match, but this isn't built in Perl5 (will be in Perl6).
--]
Of course, this all only matters if working with large lists :-)
In addition,
a third way would be to reverse first and map looking like
use List::Util qw/first/;
my $re = qr/(ARIN|APNIC)/;
my ($src) = map {/$re/}, first {$re} @ret;
allthough it is slightly odd to use map on just a 1element list.
Hope, I didn't confuse you too much,
Greetings,
Janek
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/