Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: question regarding slices

by tcf22 (Priest)
on Nov 06, 2003 at 18:44 UTC ( [id://305114]=note: print w/replies, xml ) Need Help??


in reply to question regarding slices

You could use grep, like this
foreach(grep ! /^\.{1,2}$/, readdir(PRINT)){ #do work }
but it loops through the list twice, which may be a little inefficient, so you could use glob which removes the . and .. automatically.
my $dir = '/path/to/dir'; foreach(glob('$dir/*')){ #do work }
I prefer using glob().

As for retrieving all but the first 2 elements of the list, it is a little akward, so you could assign them into an array and do this
my @array = readdir(PRINT); foreach(@array[2..$#array]){ #do work }

- Tom

Replies are listed 'Best First'.
Re:x2 question regarding slices (don't use regexps to filter . and ..)
by grinder (Bishop) on Nov 06, 2003 at 21:54 UTC

    Don't use grep to filter out these entries. Unless you know what you are doing, you open yourself to security problems (missing a directory you should traverse). And if you do know what you are doing, it looks strange to someone who doesn't, and so they might want to "fix" it and hence break it.

    More discussion about this issue can be found at:

    And in case the OP isn't aware of them, this might be the right moment to mention File::Find and File::Find::Rule.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://305114]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-19 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found