in reply to While loops with if

Ok, thanks everyone. I took out the semicolon and now the script sort of runs. I added the semicolon because I thought that using an if in a while loop would be different so I thought I'd have to terminate that line otherwise the rest of the loop wouldn't know what it's doing. I have a problem now though, the script is running but it's printing to screen an infinite times (Your password is: ). It's not giving out a password so my guess is it's having problems opening up USEDPW but it's not displaying any warnings with that.

Any suggestions?

Thanks
#!/usr/bin/perl use CGI::Carp 'fatalsToBrowser'; use strict; use warnings; use CGI; my $query = CGI->new; print $query->header; my $usedpw = "logfile.txt" ; my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) ); my $pw = join @chars[map{rand @chars} (1..17)]; # opening for reading open(USEDPW, "< $usedpw") or die $!; flock USEDPW, 1; close(USEdPW); while (1) { my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) ); if ($usedpw ne m/$pw/) { print "Your unique password is: $pw\n"; } } open(USEDPW, "> $usedpw") or die $!; flock USEDPW, 2; print USEDPW "$pw\n"; close(USEDPW);


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Re: While loops with if
by jdporter (Paladin) on Jan 03, 2003 at 19:22 UTC
    The program is doing what you told it to do. It is looping forever, because you didn't include any way for the loop to end. And it's printing a null password because you never generate a password. The point of the loop is to generate passwords, and keep generating them, UNTIL one is found which has not been generated before.

    Please read this and try to understand.
    # define these outside the loop, please. my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) ); # load the previously generated passwords from the file: my $usedpw = "logfile.txt" ; open USEDPW, "< $usedpw" or die "Error opening $usedpw for reading: +$!\n"; flock USEDPW, 1; # shared lock my @used_pw = <USEDPW>; close USEDPW; chomp @used_pw; # and put them in a set (implemented as a hash): my %used_pw; for ( @used_pw ) { $used_pw{$_}++ } my $pw; do { # create a password: $pw = join '', @chars[map{rand @chars} (1..17)]; } while exists $used_pw{$pw}; # we now have a password which has not been used before, so report i +t print "Your unique password is: $pw\n"; # and store it in the file. open USEDPW, "> $usedpw" or die "Error opening $usedpw for writing: +$!\n"; flock USEDPW, 2; # exclusive lock print USEDPW "$pw\n"; close USEDPW;

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

Re: Re: While loops with if
by chromatic (Archbishop) on Jan 03, 2003 at 19:15 UTC

    Please take the advice of other posters and fix your indentation. It will make your life much easier.

    The easiest answer as to why your password doesn't match is because you never read anything from the password file. Inside your loop, $usedpw is still set to 'logfile.txt'. I suspect you want something more like this:

    open USEDPW, 'logfile.txt' or die "Cannot read logfile: $!\"; my $seen; while (<USEDPW>) { chomp; $seen++, last if $pw eq $_; } print "Your unique password is '$pw'\n" unless $seen;

    I'm not sure that's what you really want, though... and it'd be more efficient to use a DBM file.