L
LANkrypt0
I wrote the following little script below and am running into a problem.
The script with an argument:
../script.pl hello
It then would get the length of the word hello and use that to generate
random letters of that amount. i.e, doiqw
Then it SHOULD keep running until the word hello is matched with a one of
the random outputs. The problem is that it outputs the same results, i.e
doiqw
doiqw
doiqw
etc.
Any help or insight into why this is happening would be greatly
appreciated. Also any tips on better coding practices or streamlining
this is also appreciated =)
Thanks!
==+ BEGIN CODE +==
#!/usr/bin/perl
use strict;
my $counter = 0;
my $attempts = 0;
my $word;
my $tword;
sub wordcheck #Checks if word matches, if so print and exit
{
if ($word eq $ARGV[0])
{
print "Word found in $attempts attempts!\n";
exit
}
}
sub getword #Generates random letters, length based on $ARGV[0] length
{
while ($counter < length($ARGV[0]))
{
$tword = chr(int(rand(ord("z")-ord("a"))+ord("a")));
$word = $tword.$word;
$counter++;
}
}
while ($word ne $ARGV[0]) #Runs until matches
{
getword
print "$word\n";
wordcheck
$attempts++
}
==+ END CODE +==
The script with an argument:
../script.pl hello
It then would get the length of the word hello and use that to generate
random letters of that amount. i.e, doiqw
Then it SHOULD keep running until the word hello is matched with a one of
the random outputs. The problem is that it outputs the same results, i.e
doiqw
doiqw
doiqw
etc.
Any help or insight into why this is happening would be greatly
appreciated. Also any tips on better coding practices or streamlining
this is also appreciated =)
Thanks!
==+ BEGIN CODE +==
#!/usr/bin/perl
use strict;
my $counter = 0;
my $attempts = 0;
my $word;
my $tword;
sub wordcheck #Checks if word matches, if so print and exit
{
if ($word eq $ARGV[0])
{
print "Word found in $attempts attempts!\n";
exit
}
}
sub getword #Generates random letters, length based on $ARGV[0] length
{
while ($counter < length($ARGV[0]))
{
$tword = chr(int(rand(ord("z")-ord("a"))+ord("a")));
$word = $tword.$word;
$counter++;
}
}
while ($word ne $ARGV[0]) #Runs until matches
{
getword
print "$word\n";
wordcheck
$attempts++
}
==+ END CODE +==