> -----Original Message----- > From: Michael D. Risser [mailto:[EMAIL PROTECTED]] > Sent: Thursday, November 08, 2001 11:01 AM > To: [EMAIL PROTECTED] > Subject: Comparison problem > > > I have a script which in part takes a list of files in a > directory (the name > of each file is a number), sorts them numerically, and then > runs through each > file checking to see if any are missing, and performing a set > action if it is > missing, or a different action if it is present. > > My problem is my script is only performing the "missing" > action, regardless > of whether or not the file is present. > > Could some one tell me where I went wrong? > > chdir($ARGV[0]) or die "Unable to enter directory: $!\n\n\n"; > > # Get a list of filenames to process > opendir(THIS, '.') or die "Cannot open directory: $!\n\n\n"; > my @filenames = readdir(THIS) or die "Unable to get list of > files: $!\n\n\n"; > > # Need to sort the filenames so that they get processed in > numeric order; > # this ensures that bugs are automatically assigned the right bug ID # > > my @fileNames = sort {$a <=> $b} @filenames; > > # Get the number of files to process > my $numFiles = @fileNames; > > #|--Remove Before Flight--> > print "Number of files to process: $numFiles\n"; > print "Press any key to continue, Ctrl-C to exit\n"; > chomp(my $in=<STDIN>); > #<--Remove Before Flight--| > > my $j = 1; > # Go through each file we found > for(my $i = 0; $i <= $numFiles;$i++) { > if ($file != $j) { > print "Sending missing for file: $i"; > &sendMissing; > $j++; > next; > } > }
Where is $file set? If you're trying to set it to $fileNames[i], you're going to have a problem, because @fileNames will have the names of directories as well as files (including '.' and '..') So @fileNames may look like: [0] = "." [1] = ".." [2] = "0" [3] = "1" See the flaw? Also, if a file is missing, $numFiles will be the number of files actually there, which is less than the number you need to check. An alternate approach might be to use -f to test for file existence: for (0 .. $max) { if (-f) { print "$_ exists\n"; } else { print "$_ doesn't exist\n"; } } This avoids having to read the files into an array. But how do you determine $max? From the files that are there? Fine, but what if the last file is missing? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]