in reply to Random Text: There's got to be an easier way

The Cookbook mentions this on page 52:
my @chars = ("a".."z","A".."Z"); my $random_string = join("",@chars[map{ rand @chars} (1..8)])); #8 being the number of chars to randomize ofcourse
Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur.

Replies are listed 'Best First'.
Re: Re: Random Text: There's got to be an easier way
by sierrathedog04 (Hermit) on Jun 03, 2001 at 22:07 UTC
    Beatnik nailed it, but he left out the numerals 2 through 9 and added an extra parenthesis to the second line. The exact solution is thus:
    use strict; my @chars = ("a".."z","A".."Z", 2..9); my $random_string = join("",@chars[map{ rand @chars} (1..8)]); #8 being the number of chars to randomize ofcourse print "$random_string\n";
      The code described in the Cookbook also includes some non-alphanumeric chars...
      I'm pretty confident the code mentioned above is faster than other stuff (but then again, I didn't write it so that's normal).

      BTW you left out 1 :)

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.
Re: Re: Random Text: There's got to be an easier way
by Hero Zzyzzx (Curate) on Jun 03, 2001 at 21:27 UTC

    Thanks!

    Funny. It must have just been the quotes that I was missing.

    Another reason to read the Cookbook, which I just recently purchased (along with Programming Apache Modules with Perl and C)

    Have I got some reading (and re-reading) to do. . .