Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with readdir

Status
Not open for further replies.

edegner

IS-IT--Management
Aug 5, 2005
2
US
I'm having a problem that I can't figure out. Below is a snippet of my code that's not functioning the way I believe it should..

opendir (PIX_DIR, ".") or die $!;
foreach $file (readdir(PIX_DIR)) {
print "$file\n";
if ($file =~ /^\./ || $file == '') {
next;
}
print "$file\n";
}

I would expect it to display each of the filenames first unless it started with . but it's not getting to the second print statement, or anything else after that in my script. It's just spitting out each filename once, like below.

img_5870.jpg
img_5871.jpg
img_5872.jpg
img_5873.jpg
img_5874.jpg
img_5875.jpg
img_5876.jpg

Any help would be greatly appreciated. Thanks.
 
Try this:
Code:
#!/usr/bin/perl

use strict;
use warnings;
my $PIX_DIR;
opendir ($PIX_DIR, ".") || die "Can't open $PIX_DIR: $!";

foreach my $file (readdir($PIX_DIR)) {
   next if $file =~ /^\.\.?$/;    
   print "$file\n";
}

Let us know your results!

X
 
The problem with your code is that you are trying to do a string comparison with the numeric operator (==). Your code will work if you change the '==' to 'eq'. See 'Equality Operators' in the perlop perldoc.

Code:
if ($file =~ /^\./ or $file eq '') {
        next;
}

That said, you shouldn't have to check for null file names anyway, as no operating system would allow them. The test statement in Xaqte's code is sufficient.

jaa
 
HI edenger

A slight mod to your script shows what is going wrong:-

Code:
[b]#!/usr/bin/perl[/b]

@files = qw( file1.txt file2.txt .file3.txt file4.txt .file5.txt file6.txt );

foreach $file (@files) {
    print " FIRST PRINT STATEMENT: $file\n"; [red]this will always be actioned[/red]
    if ($file =~ /^\./) {  [red]this will be actioned if the file begins with a period[/red]
        print "... just about to 'next'\n";
        next;
    }
    print "SECOND PRINT STATEMENT: $file\n"; [red]this is skipped [b]only[/b] if the 'next' was actioned[/red]
}

outputs:-

FIRST PRINT STATEMENT: file1.txt
SECOND PRINT STATEMENT: file1.txt
FIRST PRINT STATEMENT: file2.txt
SECOND PRINT STATEMENT: file2.txt
FIRST PRINT STATEMENT: .file3.txt
... just about to 'next'
FIRST PRINT STATEMENT: file4.txt
SECOND PRINT STATEMENT: file4.txt
FIRST PRINT STATEMENT: .file5.txt
... just about to 'next'
FIRST PRINT STATEMENT: file6.txt
SECOND PRINT STATEMENT: file6.txt


Kind Regards
Duncan
 
Xaqte thanks for the welcome and for the help. I'm almost there. I made the changes you suggested, but had to slightly modify it because the script wouldn't run at all. Now it's function almost how it should. At the end of script execution I get the following...

Use of uninitialized value in ref-to-glob cast at ../addimage.pl line 79.
Bad symbol for filehandle at ../addimage.pl line 79.

I even added justice41's tip of or $file eq '' but I still get the same error.

It's weird, because the way I had it originally USED to work just fine. I had made a few changes to the script to write to a different table in the database, and copy the files to a different directory, but the only thing I changed was the table name and the directory names, everything else remained exactly the same, but that part of the script stopped working.
 
you'll have to post line 79 with enough surrounding context for us to help. You almost certainly don't intend to cast a ref into a glob so I'd be looking for a syntax error.

f

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top