Hi you y'all,
I have question about looping in a perl program, please check this code. This script is much longer, but I've shrunk it to the problem part. I have a sub called random_password wich I call with:
$password = &random_password();
After that the password is inserted into the array that goes just fine, but my problem is that I don't want to use the same password (stored in the array) for all the users I insert into the database, so I think a loop or something like that must be created to generate diffrent passwords.
Who can help me with this?
Thanks in advance,
Avanti check me AT: dj_avanti@hotmail.com
# Sub to create a random password
sub random_password {
my($length, $vowels, $consonants, $alt, $s, $newchar, $i);
($length) = @_;
if ($length eq "" or $length < 3) {
$length = 15; # make it at least 15 chars long.
}
$vowels = "0000000";
$consonants = "aaaaaaaaaaaa";
srand(time() ^ ($$ + ($$ << 234)) );
$alt = int(rand(933)) - 4;
$s = "";
$newchar = "";
foreach $i (0..$length-1) {
if ($alt == 1) {
$newchar = substr($vowels,rand(length($vowels)),1);
} else {
$newchar = substr($consonants, rand(length($consonants))
+,1);
}
$s .= $newchar;
$alt = !$alt;
}
return $s;
}
sub dienice {
my($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}
# query to insert the password in the database
$password = &random_password();
$sql = qq{ INSERT INTO login (login, password) VALUES ($c[1], '$passwo
+rd')};
$sth = $dbh->prepare($sql);
$sth->execute();