LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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

Reply
 
LinkBack Search this Thread
Old 09-09-2006, 11:32 PM   #1
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Rep: Reputation: 15
Perl: Using readdir and . ..


Just a simple question. Anyway to keep the . and .. from showing up in an array when I use readdir?
 
Old 09-09-2006, 11:55 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 233Reputation: 233Reputation: 233
An example
Code:
@dirs = readdir(DIR) or die .....
closedir(DIR);
foreach $d (dirs) {
    next if ($d eq "."); 
    next if ($d eq "..");
    ....
    ....
 
Old 09-12-2006, 02:34 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.7, Solaris 10
Posts: 10,994

Rep: Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752
Or slightly more compact / perlish

next if $file =~ /^\.\.?$/;
 
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: 686

Rep: Reputation: 61
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.
 
Old 09-13-2006, 07:21 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 233Reputation: 233Reputation: 233
I would think if its for readability , no regular expression is better.
 
Old 04-23-2008, 11:34 AM   #6
treii28
LQ Newbie
 
Registered: Apr 2008
Posts: 1

Rep: Reputation: 0
or another way

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

next if $file =~ /^\.\.?$/;
next if $file =~ /^\.{1,2}$/;
 
Old 04-23-2008, 07:04 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 5.7, Solaris 10
Posts: 10,994

Rep: Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752Reputation: 752
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
 
Old 04-24-2008, 04:25 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,016

Rep: Reputation: 91
even more perlish?

Code:
@dirs = grep { !/^\.\.?/ } readdir(DIR);
 
  


Reply

Tags
expression, perl, regular


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are 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 10:52 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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration