in reply to Re: Re: Generating a random string of chars, easily.
in thread Generating a random string of chars, easily.
Sure. This is the same subroutine but opened out and using named parameters to explain the intermediate steps.
sub rndStr{ # When an array is interpolated in a string $" is used to seperate + the values # By setting it (locally) to '', we ensure that the string doesn't + have spaces or # anything else inserted between the characters. local $" = ''; my ( $len, @chars_to_pick_from) = @_; # @picks will end up with as many random values as requested. my @picks = map{ # Generate random numbers bewteen 0 and # the number of chars supplied to pick from rand $#chars_to_pick_from # do it as many times as request } 1 .. $length; # Using an array slice interpolated in quotes to construct the str +ing # @picks contains the random indices, the slice picks them out of +the # array of choices past in. The resultant string is returned as a +scalar. "@chars_to_pick_from[@picks]"; }
In the short version above, I'm just using the @_ array, (and shift to get the first parameter) and doing the whole thing in a single step rather than all seperated out.
Hope that helps.
Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Generating a random string of chars, easily.
by Sihal (Pilgrim) on Nov 14, 2002 at 18:25 UTC |