desertrat has asked for the wisdom of the Perl Monks concerning the following question:
I don't even know if I'm on the right track here, but...
I have an account creation system that autogenerates passwords. We have recently changed policies so that a password needs to include at least one each from upper and lower case letters, numbers and non-numeric characters.
Just randomly generating passwords from that whole set is simple, but making sure at least one of each set is included and their inclusion is random in the password ie just selecting random pairs form each in line aaBB22%% is not acceptable.
So my strategy will be to shuffle an array of references then use the following scheme to build pieces of the password:
join("", @array[map {rand @array}(1..n)]);
but when I try to plug in array references, I get just one character from each array:
#!/usr/bin/perl use strict; use warnings; my @chars = ( "a" .. "k", "m","n", "p" .. "z"); my @upchars = ("A".."N","P..Z"); my @nona = qw(! @ $ % & ( ) + = { } [ ] < > ~); my @nums = (0 .. 9); my $lc = \@chars; my $uc = \@upchars; my $na = \@nona; my $nu = \@nums; my @types=($lc, $uc, $na, $nu); my $a = join("", $types[3][ map { rand \$types[3] } ( 1 .. 4 ) ]); $a .= join("", $types[2][ map { rand \$types[2] } ( 1 .. 4 ) ]); $a .= join("", $types[1][ map { rand \$types[1] } ( 1 .. 4 ) ]); $a .= join("", $types[0][ map { rand \$types[0] } ( 1 .. 4 ) ]); print "My selection is $a \n"; exit;
When I run that I get the result '4&Ee', if I reverse the array indexes thus:
my $a = join("", $types[0][ map { rand \$types[0] } ( 1 .. 4 ) ]); $a .= join("", $types[1][ map { rand \$types[1] } ( 1 .. 4 ) ]); $a .= join("", $types[2][ map { rand \$types[2] } ( 1 .. 4 ) ]); $a .= join("", $types[3][ map { rand \$types[3] } ( 1 .. 4 ) ]);
I get the result 'eE&4' so I know I'm pulling one character from the arrays.
It makes no difference if the map part is map { rand \$types[1] } ( 1 .. 4 ) or map { rand $types[1] } ( 1 .. 4 )
What am I doing wrong and am I even on the right track?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Handling elements of an array of array references, where the function calls for an array?
by Perlbotics (Archbishop) on May 14, 2012 at 22:03 UTC | |
|
Re: Handling elements of an array of array references, where the function calls for an array?
by Anonymous Monk on May 14, 2012 at 20:42 UTC | |
|
Re: Handling elements of an array of array references, where the function calls for an array?
by mbethke (Hermit) on May 14, 2012 at 20:47 UTC | |
by desertrat (Sexton) on May 14, 2012 at 21:41 UTC | |
|
Re: Handling elements of an array of array references, where the function calls for an array?
by johngg (Canon) on May 14, 2012 at 22:56 UTC | |
by desertrat (Sexton) on May 14, 2012 at 23:49 UTC |