Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Problem in displaying files

by Anonymous Monk
on Dec 08, 2012 at 16:19 UTC ( #1007903=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
I wrote a simple script to display a list of files in a directory:-

opendir(DIR, $dirname); @FILES= readdir(DIR); foreach $FILE (@FILES){ print $FILE."\n"; } closedir DIR;
The files in the directory are f1 f2 and g3. But I'm getting an output as:-
What are the dots(. and ..) in the beginning,this is becoming a problem when I want to open files from a directory and perform manipulations on them as the . and .. files cannot be opened.

Replies are listed 'Best First'.
Re: Problem in displaying files
by moritz (Cardinal) on Dec 08, 2012 at 16:23 UTC

      Thank you, that solved my problem.

Re: Problem in displaying files
by eyepopslikeamosquito (Chancellor) on Dec 08, 2012 at 22:28 UTC

    I normally use grep for this:

    @files = grep { $_ ne '.' && $_ ne '..' } readdir DIR;

Re: Problem in displaying files
by Vijay81 (Acolyte) on Dec 08, 2012 at 17:51 UTC
    you can also try this logic :
    while($file = readdir($dh)) { next if $file eq '.'; next if $file eq '..'; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1007903]
Approved by moritz
[LanX]: and disoplay the data permanently in your peronal nodelet
[marinersk]: I saw that you could post it on the Personal Nodelet, but you have to provide the content also. No way to auto-populate it. Not that this would solve my distraction problem...
[LanX]: there are template variables to "Auto-populate" it
[marinersk]: Yeah, someday I might become interested enough to poke around PerlMonks more internally. Most of the fancy stuff is way down on my to do list.
[LanX]: Yeah ...
[LanX]: no variable for votes_left
[marinersk]: Ah, well. I will continue to deal with it, as I have. Thanks for poking about, though.
[LanX]: you are welcome! :)

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2017-05-29 14:25 GMT
Find Nodes?
    Voting Booth?