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

Hello Monks,

What is the best way of randomly choosing letters from the alphabet?

I am trying to automate mounting drives on my computer.

And, I want to randomly choose an alphabetic letter that does not match C: or D: etc.

Thank you

Replies are listed 'Best First'.
Re: randomly choosing an alphabetic letter
by japhy (Canon) on Mar 15, 2005 at 21:40 UTC
    Sounds like you want:
    use Win32; my $next_drive = Win32::GetNextAvailDrive();
    If you have drives "A:" through "D:", this returns "E:".
    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Hello,

      If I would like to choose 4 free drives, and I run the Win32::GetNextAvailDrive() 4 times, it gives me the same drive everytime, not different ones.

      Is there a way I can find out more than one available drive?

      Thanks

        What you would want to do, then, is use each one in a loop, rather than getting them ahead of time. That is, rather than:

        my @drives = ( Win32::GetNextAvailDrive(), Win32::GetNextAvailDrive(), Win32::GetNextAvailDrive(), Win32::GetNextAvailDrive(), ); do_stuff_with(@drives);
        do this:
        for (1..4) { my $drive = Win32::GetNextAvailDrive(); do_stuff_with($drive); }
        Hopefully, do_stuff_with() will net use or something so that the drive becomes unavailable (in use) so the next time through, Win32::GetNextAvailDrive will get a new drive.

      Thank you all for your replies
Re: randomly choosing an alphabetic letter
by Tanktalus (Canon) on Mar 15, 2005 at 21:28 UTC
    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.

      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

Re: randomly choosing an alphabetic letter
by jZed (Prior) on Mar 15, 2005 at 21:28 UTC
    my $random_upper_case_letter = chr(int(rand(26)+1)+65);
Re: randomly choosing an alphabetic letter
by 5mi11er (Deacon) on Mar 16, 2005 at 17:03 UTC
    From a batch file or command line, you can say
    net use * \\server\resource
    for windoze itself to assign to the next available drive letter...

    -Scott

A reply falls below the community's threshold of quality. You may see it by logging in.