Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: $str to %hash to @ary

by ccn (Vicar)
on Jul 22, 2004 at 21:17 UTC ( [id://376720]=note: print w/replies, xml ) Need Help??


in reply to $str to %hash to @ary

my $str="17:43:33:21:23:19:27:6"; my @aux = split /:/, $str; my @ary = map {($aux[2*$_+1]) x $aux[2*$_]} 0 .. @aux/2-1;

UPDATE:
You can try the following code also:

my $str="17:43:33:21:23:19:27:6"; my %hash = split /:/, $str; my $adno; my $rand = rand 100; my $sum = 0; for (keys %hash) { # there is no need of sorted keys $adno = $hash{$_}; last if ($sum += $_) > $rand; } print $adno;

UPDATE: sort keys eliminated in 'for'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-18 17:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found