in reply to To register or not (part 2)
But the error there is pretty obvious: you return from password on the very first iteration through the for loop. I'm guessing that what you really want to do is accumulate all those things in an array, and return them at the end:sub password { my( $s ) = @_; srand( $s ^ time ); my @c = split / */, "bcdfghjklmnpqrstvwxyz"; my @v = split / */, "aeiou"; for ( my $i = 1; $i <= 4; $i++ ) { return( $c[ rand @c ], $v[ rand @v ] ); } }
This can be made even more idiomatic by using map:sub password { my( $s ) = @_; srand( $s ^ time ); my @c = split / */, "bcdfghjklmnpqrstvwxyz"; my @v = split / */, "aeiou"; my @result; for ( my $i = 1; $i <= 4; $i++ ) { push @result, $c[ rand @c ], $v[ rand @v ]; } return @result; }
Other things I did to make it "better":sub password { my( $s ) = @_; srand( $s ^ time ); my @c = split / */, "bcdfghjklmnpqrstvwxyz"; my @v = split / */, "aeiou"; return map { $c[ rand @c ], $v[ rand @v ] } 1..4; }
jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.
|
|---|