Gossamer Forum
Home : General : Perl Programming :

while loop

Quote Reply
while loop
Hi Folks,

Just passing on to a subroutine to you folks to see if you can spot something wrong, because I certainly can't! The subroutine is suppose to generate a random number and then open up every file within a directory and check whether the first field of any file uses that number. If the number is taken the script should go back and look for another random number but it doesn't seem to bother and the script seems to not mind if the number is taken. And it's starting to really annoy me. Have you kind folks got any suggestions for me?

BTW: Good choice dumping the UBB. That program was written by someone with the same level of understanding perl as I, which is not good. Good call going for the SQL version as it seems these forums were going huge! Keep up the good work!

sub generate_random_key {

srand();
$randnum = 3;
$randkey = int rand $randnum;

opendir (DIR,"$DATA_DIR/eng/");
@files = grep (!/^\.\.?$/, readdir (DIR) );
closedir (DIR);

$delimiter = "\n\%\%\n";

foreach $file (@files) {

open (FILE,"$DATA_DIR/eng/$file");
@filedata = <FILE>;
close (FILE);

$fields = join('',@filedata);

@fields = split(/$delimiter/,$fields);

$dat_id = $fields[1];

while (dat_id eq "$randkey") {
$randkey = int rand $randnum;
}

}


}

I've set the random number value as low as 3 only for testing purposes.

Thanks!

Wil.