in reply to Re^4: Help with sorting/randomizing?
in thread Help with sorting/randomizing?

A problem with the above is that after the first valid IP address has been defined, there is nothing that explicitly undefines it, so the following line becomes useless:

next unless defined $ip;

The minimal change you could make to fix this bug would be to add $ip = $classc = undef; just before the if block.

You could improve it further by use strict combined with putting a bit of thought into the scoping of variables.

It can be reduced to:

use 5.010; use strict; use warnings; my %ips; while (<$in_fh>) { if (/^(\d+)\.(\d+)\.(\d+)\.(\d+)/) { my $ip = "$1.$2.$3.$4"; my $classc = "$1.$2.$3"; push @{ $ips{$classc} }, $ip; } } foreach my $classc (sort keys %ips) { say $ips{$classc}[rand @{$ips{$classc}}]; }

Personally I'd go further and reduce it even more. This might be a little too terse for some people's tastes though...

use 5.010; use strict; use warnings; my %ips; while (<$in_fh>) { push @{ $ips{"$1.$2.$3"} }, "$1.$2.$3.$4" if /^(\d+)\.(\d+)\.(\d+)\.(\d+)/; } say $ips{$_}[rand @{$ips{$_}}] for sort keys %ips;
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'