Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Help with sorting/randomizing?

by ashish.kvarma (Monk)
on Mar 26, 2012 at 04:39 UTC ( [id://961581]=note: print w/replies, xml ) Need Help??


in reply to Help with sorting/randomizing?

I probably should not give the complete code here as it goes against the the motto but couldn't resist the temptation in this case.

I know there can be better solution to the problem in based on scenarios but here is my take on the issue.

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 = <DATA>) { 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
Update:

I want these IPs randomized by first Class A, then Class-B, then Class-C.

Looking back at the problem statement,,, may not be exactly what was expected. Here is what it prints:

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
Regards,
Ashish

Replies are listed 'Best First'.
Re^2: Help with sorting/randomizing?
by countingcrows (Initiate) on Mar 26, 2012 at 06:41 UTC
    Ashish, thanks. But main thing I am trying to avoid is to have sequential class-c's which is abundant in your code. However, your code is giving me some clues to work on.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://961581]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found