LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 09-09-2006, 10: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, 10:55 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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, 01:34 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

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

next if $file =~ /^\.\.?$/;
 
Old 09-13-2006, 09:52 AM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
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 12:03 PM.
 
Old 09-13-2006, 06:21 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

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, 06:04 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 03:25 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
even more perlish?

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


Reply

Tags
expression, perl, regular



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-16-2005 11:26 PM
Regarding readdir r_213 Linux - Networking 2 01-15-2005 07:56 AM
readdir then read the files jupe Programming 2 07-13-2004 10:04 AM
after readdir how can I open all the files jupe Programming 1 07-13-2004 09:00 AM
using readdir to display files sutho1 Programming 2 10-10-2003 09:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:40 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration