m0ve has asked for the wisdom of the Perl Monks concerning the following question:

hey monks,
i got a little problem with my hash :

the hash looks like this :
$Ordnung{$arzt_nr}{$vknr}{$versnummer}{ziffer}
for each $arzt_nr i need to pick 15 random $versnummers.
there are only 2 different possibilities for "ziffer"
there might be not enough $versnummers sometimes then i just take all of them, and each $versnummer{ziffer} may be one of the 15, but when there are more then 15 i need to pick different $versnummers before picking the same ones with different "ziffers"

would be great if anyone had an idea on how to solve this, somehow i'm at a loss with this problem...

Replies are listed 'Best First'.
Re: random picks from hash
by moritz (Cardinal) on Aug 29, 2007 at 09:34 UTC
Re: random picks from hash
by Anonymous Monk on Aug 29, 2007 at 09:32 UTC
    use strict; use warnings; my @arr; my %has = qw[ a b c you know me moo moo ]; @arr = keys %has; warn $has{ $arr[ rand @arr ] } for 1..10; __END__
Re: random picks from hash
by Anonymous Monk on Aug 29, 2007 at 09:39 UTC
    for my $arzt_nr ( keys %Ordnung ){ my @ver = keys %{ $Ordnung{$arzt_nr}{$vknr}{$versnummer} }; for (1..15){ my $verrand = $ver[ rand @ver] ; warn $Ordnung{$arzt_nr}{$vknr}{$verrand}{ziffer}; } }
      well i'm tryin to get 15 examples for each $arzt_nr....
      currently i have this :

      open(OUTPUT, $file3) || die "Datei nicht gefunden"; #outputfile öf +fnen foreach my $arzt_nr (keys %Ordnung){ my @versnummer = (keys %{ $Ordnung{$arzt_nr}{$vknr}}); for (1..15){ my $verrand = $versnummer[rand @versnummer] ; print OUTPUT "$arzt_nr;"; print OUTPUT "$Ordnung{$arzt_nr}{$vknr}{$verrand}{bht};"; print OUTPUT "$Ordnung{$arzt_nr}{$vknr}{$verrand}{gnr};"; print OUTPUT "$Ordnung{$arzt_nr}{$vknr}{$verrand}{Name};"; print OUTPUT "$Ordnung{$arzt_nr}{$vknr}{$verrand}{Vorname} +;"; print OUTPUT "$Ordnung{$arzt_nr}{$vknr}{$verrand}{Geburtst +ag};"; print OUTPUT "$vknr;"; print OUTPUT "$_\n"; } } close (OUTPUT); }
      it's almost fine when there are more then 15 possibilities (get the same output twice sometimes), but i still got problems if there are less....tryin to figure that out atm 8)

      thx for the help, especially the 2nd ano monk's post was very helpful