in reply to Variable still not holding contents

Looks like jdporter covered the major points, except for the fact that your use of "join()" (with the embedded "map" and "rand") won't work, either.

Slow things down a bit, give yourself some space, and use a loop to generate the candidate password -- put it in a subroutine, maybe, to keep it clear:

... my $pw; do { $pw = make_new_pw(); } while ( grep { $pw eq $_ } @stored ); ... # update: moved the declaration of @chars into the subroutine, # where it belongs: sub make_new_pw { my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw( ! @ $ % ^ & *) ) +; my $newpw = ""; for ( 1 .. 17 ) { $newpw .= $chars[ rand @chars ]; } return $newpw; }
One closing tip: I've seen people get really bolixed because they are sent a password like "lXIg1v0WO" -- given the chance to control which letters are available to go into a random password, I would tend to leave "I1l" and "0O" out of the set, because they are too easily confused and misread.

Replies are listed 'Best First'.
Re: Re: Variable still not holding contents
by jdporter (Paladin) on Jan 07, 2003 at 14:26 UTC
    Good catch!
    Actually, the only problem with his code as given was the missing join character as the first argument to join. Inserting a null string:   $pw = join('',@chars[map{rand @chars} (1..17)]; This works, because map{rand @chars} (1..17) does indeed generate a list of 17 indices, each in the proper range for the @chars array. (They're floating point, but they get intified when used as indices.)
    That said, it's not how I would have done it. I would have written   $pw = join '', map { $chars[ rand @chars ] } 1..17;

    jdporter
    The 6th Rule of Perl Club is -- There is no Rule #6.

      ...the only problem with his code as given was the missing join character...

      And then there's the missing close paren that needs to be added after the close square bracket. Anyway, I agree that you're latter approach is clean and easy enough -- still, I think that having a subroutine with a name that indicates what the **** is going on is a really nice feature.