LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Tags used in this thread
Popular LQ Tags , , , ,

Reply
 
Thread Tools
Old 09-09-2006, 11:32 PM   #1
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112
Thanked: 0
Perl: Using readdir and . ..


[Log in to get rid of this advertisement]
Just a simple question. Anyway to keep the . and .. from showing up in an array when I use readdir?
cramer is offline     Reply With Quote
Old 09-09-2006, 11:55 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,814
Blog Entries: 5
Thanked: 115
An example
Code:
@dirs = readdir(DIR) or die .....
closedir(DIR);
foreach $d (dirs) {
    next if ($d eq "."); 
    next if ($d eq "..");
    ....
    ....
ghostdog74 is offline     Reply With Quote
Old 09-12-2006, 02:34 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.4
Posts: 7,419
Thanked: 325
Or slightly more compact / perlish

next if $file =~ /^\.\.?$/;
chrism01 is offline     Reply With Quote
Old 09-13-2006, 10:52 AM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Solaris, SuSE, Xandros
Posts: 566
Thanked: 3
Hi.

The part of this topic relating to regular expressions reminded me of the section in Conway's Perl Best Practices. If I understand Conway, he would suggest writing the RE like this:
Code:
next if $i =~ m{ ^ [.] [.]? $ }xms;
I think his approach is useful: balance readability and maintainability with speed. I cannot say that my fingers are yet familiar typing the braces (especially for multi-line patterns) and then the xms modifiers after every match expression, but it's not as foreign as it once was. It is clearly more typing, but I agree that the single-character classes are more readable compared to leaning toothpicks.

In addition to readability, he also writes about issues affecting performance.

If one does a lot of perl, I think it's a useful book, particularly if one is writing code that someone else may need to maintain. Many people in the local perl-mongers group seem to like it ... cheers, makyo
( 37 )

Last edited by makyo; 09-13-2006 at 01:03 PM..
makyo is offline  
Tag This Post , , , ,
Reply With Quote
Old 09-13-2006, 07:21 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,814
Blog Entries: 5
Thanked: 115
I would think if its for readability , no regular expression is better.
ghostdog74 is offline     Reply With Quote
Old 04-23-2008, 11:34 AM   #6
treii28
LQ Newbie
 
Registered: Apr 2008
Posts: 1
Thanked: 0
or another way

Quote:
Originally Posted by chrism01 View Post
Or slightly more compact / perlish

next if $file =~ /^\.\.?$/;
next if $file =~ /^\.{1,2}$/;
treii28 is offline     Reply With Quote
Old 04-23-2008, 07:04 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.4
Posts: 7,419
Thanked: 325
Well, given it's Perl, TMTOWTDI. As it happens, I got my version from the Perl CookBook (Recipe 9.5), and if it's good enough for them, it's def good enough for me
chrism01 is offline     Reply With Quote
Old 04-24-2008, 04:25 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,709
Thanked: 19
even more perlish?

Code:
@dirs = grep { !/^\.\.?/ } readdir(DIR);
bigearsbilly is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
about readdir r_213 Programming 1 01-17-2005 12:26 AM
Regarding readdir r_213 Linux - Networking 2 01-15-2005 08:56 AM
readdir then read the files jupe Programming 2 07-13-2004 11:04 AM
after readdir how can I open all the files jupe Programming 1 07-13-2004 10:00 AM
using readdir to display files sutho1 Programming 2 10-10-2003 10:09 PM


All times are GMT -5. The time now is 08:22 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration