in reply to randomly choosing an alphabetic letter

my @letters = 'E' .. 'Z'; my $drive = $letters[rand scalar @letters];

As you remove letters (say, using splice from @letters), this would continue to work.

Update: Yes, as Limbic~Region points out, the scalar is probably superfluous. I just like being explicit - especially when I'm not looking at the docs to see what the parameters really are. ;-)

Update2: Typo - had slice rather than splice. Thanks to Limbic~Region for pointing it out.

Replies are listed 'Best First'.
Re^2: randomly choosing an alphabetic letter
by Limbic~Region (Chancellor) on Mar 15, 2005 at 23:46 UTC
    Tanktalus,
    The scalar is superfluous. I would probably use a closure that knows to refill @letters once it is empty if I was going the splice route. OTOH, if I didn't need to remove elements I probably wouldn't use the array at all:
    my $drive = chr( int(rand 26) + 65 );

    Cheers - L~R