use strict; use warnings; use Data::Dumper; my %ips_hash; my $group = ''; # Create the data structure as # %h = ( # A => { # 184.75.65.68 => 1 # }, # B => { # 184.75.122.146 => 1, # 184.75.122.147 => 1, # }, # ); while (my $line = ) { if ($line =~ /#Group (\w+)/) { $group = $1; } elsif ($line =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) { $ips_hash{$group}{$1} = 1; } } # see if there is any group my $true = keys %ips_hash; while ($true) { # Get the random group my @groups = keys %ips_hash; $group = $groups[rand($#groups)]; # get the random ip my @ips = keys %{$ips_hash{$group}}; my $ip = $ips[rand $#ips]; print "$ip random from Group $group\n"; # delete the ip so that its not repeated delete $ips_hash{$group}{$ip}; # delete the group if it doesn't have any ips if ($#ips < 1) { delete $ips_hash{$group}; } $true = keys %ips_hash; } __DATA__ #Group A 184.75.65.68 #Group B 184.75.122.146 184.75.122.147 184.75.122.148 #Group C 64.3.71.98 64.3.71.99 64.3.71.100 64.3.71.106 #Group D 64.3.73.17 64.3.73.18 64.3.73.19 64.3.73.20 #Group E 66.1.73.21 66.1.73.22 66.1.73.23 #### 184.75.65.68 random from Group A 64.3.71.99 random from Group C 64.3.71.100 random from Group C 66.1.73.21 random from Group E 64.3.73.20 random from Group D 66.1.73.23 random from Group E 64.3.73.19 random from Group D 64.3.71.106 random from Group C 64.3.71.98 random from Group C 64.3.73.17 random from Group D 66.1.73.22 random from Group E 64.3.73.18 random from Group D 184.75.122.148 random from Group B 184.75.122.146 random from Group B 184.75.122.147 random from Group B