SPUG: Grep syntax
John W. Krahn
krahnj at telus.net
Fri Jun 15 04:52:16 PDT 2007
Bill Campbell wrote:
> Speaking of documentation, or lack thereof.
>
> I just ran across a snippet of perl that I don't undertand at
> all. In this, the array @a contains octets of an IP address, and
> it appears that the authoer wanted to set $start0. As nears as I
> can tell after trying various contents of @a, $start0 is changed
> to 0 if there are *ANY* non-zero elements of the array @a.
>
> $start0=1;
> grep { $start0=0 unless $_ == 0; } @a;
>
> if ( ! $start0 ) { ...
>
> It appears to me that this is a rather cryptic way of checking
> that the array is empty or contains only 0 elements, and that the
> grep command could be replaced with map or a simple loop.
Assuming that you are dealing with an IP addresse it would be simpler to use a
regular expression:
$start0 = $IP_addr =~ /^0+\.0+\.0+\.0+$/;
Or you could even use Socket::inet_aton():
$start0 = inet_aton( $IP_addr ) eq "\0\0\0\0";
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
More information about the spug-list
mailing list