Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: how do I shorten code

by Enlil (Parson)
on Apr 03, 2003 at 20:19 UTC ( [id://247869]=note: print w/replies, xml ) Need Help??


in reply to how do I shorten code

First shorter does not mean clearer (nor better), but it is possible to shorten your code to something like so:
#!d:\perl\bin\perl.exe $pth = "\\\\127.0.0.1\\bex"; opendir(Directory, $pth) or die "Cannot Open Directory"; @contents = grep { m!^[Bb].+\.txt$! #assuming .txt ending and -M "$path\\$_" > 1 } readdir(Directory); closedir(Directory);
A couple of other issues though. You do not need the splice as your regular expression would have eliminated it anyhow. and second I changed your regular expression because if you want to match a period in a regular expression you can do it one of two ways backwack it (i.e. \. )or in a character class (i.e. [.] ), the .txt in your regular expression would also match anything that ended with txt preceded by any character.

update:A shorter solution for the whole thing (untested):

#!d:\perl\bin\perl.exe my $pth = "\\\\127.0.0.1\\bex"; opendir(Directory, $pth) or die "Cannot Open Directory"; while ( $_ = readdir(Directory) ) { if ( m!^[Bb].+\.txt$! and -M "$path\\$_" > 1 ) { open ( FILE, "$path\\$_" > 1 ) or die "$!"; while ( my $line = <FILE> ) { print $line; } close(FILE); } } closedir(Directory);

-enlil

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-03-29 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found