How to do this job?

N

news.hinet.net

Direcory:
/usr/local/ap/
Filename:
1
1.lock
2
3
4
4.lock

want to list 2 and 3.
========================================================
opendir(DIR,"/usr/local/ap/");
@dots = grep {-f "/usr/local/ap/$_" && $_ !~m/\.lock/} readdir(DIR);
close(DIR);
========================================================
The @dots have 1 2 3 4 elements. I know i can use -e to check the
$filename.lock in loop.
But how to make @dots just have 2 and 3? Becaues the directory have
many files,i don't want to worse time to check the $filename.lock .
please help!

thanks!
 
L

Leon

news.hinet.net said:
Direcory:
/usr/local/ap/
Filename:
1
1.lock
2
3
4
4.lock

want to list 2 and 3.
========================================================
opendir(DIR,"/usr/local/ap/");
@dots = grep {-f "/usr/local/ap/$_" && $_ !~m/\.lock/} readdir(DIR);
close(DIR);
========================================================
The @dots have 1 2 3 4 elements. I know i can use -e to check the
$filename.lock in loop.
But how to make @dots just have 2 and 3? Becaues the directory have
many files,i don't want to worse time to check the $filename.lock .
please help!

thanks!

#!/usr/bin/perl
use strict;
use warnings;

opendir(DIR, "/usr/local/ap/");

# Grep statement additionally checks that the current file doesn't end with
..lock AND that there isn't
# a .lock file. For instance, if current file was 1.lock then ! -e
"/usr/local/ap/$_.lock" would have been
# checking for the existance of 1..lock.lock. Could get slightly confusing
results.

my @dots = grep{-f "/usr/local/ap/$_" && $_ !~ m/\.lock$/ && ! -e
"/usr/local/ap/$_.lock"}readdir(DIR);
closedir(DIR);

# This worked on my system:)
 
M

Michele Dondi

Direcory:
/usr/local/ap/
Filename:
1
1.lock
2
3
4
4.lock

want to list 2 and 3.

Quite a generic description, isn't it?
========================================================
opendir(DIR,"/usr/local/ap/");
@dots = grep {-f "/usr/local/ap/$_" && $_ !~m/\.lock/} readdir(DIR);

I guess you may want /\.lock$/, just to be sure...
close(DIR);
========================================================
The @dots have 1 2 3 4 elements. I know i can use -e to check the
$filename.lock in loop.
But how to make @dots just have 2 and 3? Becaues the directory have
many files,i don't want to worse time to check the $filename.lock .
please help!

You can use one more short-circuiting C<&&>, or C<and>; e.g.:

@dots = grep { !/\.lock$/ and -f "$dir/$_" and !-e "$dir/$_.lock" }

I moved the regex check to the front for system calls are generally
expensive. However I can give no guarantee that this will actually be
more efficient, but if you are really concerned about it (which IMHO
you shouldn't) then you can do some benchmark yourself.

As a side note there have been quite a few posts of this kind lately,
i.e. wrt reading a directory "manually" with readdir(): no real harm
done, but you may want to let perl help you with its own high level
glob() function instead:

@dots = grep { !/\.lock$/ and -f and !-e "$_.lock" } <path/*>;

This is not strictly equivalent to what you wrote, but it may be what
you're really after...


HTH,
Michele
 
M

Michele Dondi

#!/usr/bin/perl
use strict;
use warnings;

As a side note: well done!
opendir(DIR, "/usr/local/ap/");

# Grep statement additionally checks that the current file doesn't end with
.lock AND that there isn't
# a .lock file. For instance, if current file was 1.lock then ! -e
"/usr/local/ap/$_.lock" would have been
# checking for the existance of 1..lock.lock. Could get slightly confusing
results.

As a side note, well written code should be self-explanatory and only
require minimal cmts. IMHO such verbose cmts do not add to the
readability of sources. As a side note to the side note, if really
needed you may want to give a peek into

perldoc -q 'large block'


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,268
Messages
2,571,096
Members
48,773
Latest member
Kaybee

Latest Threads

Top