[28165] in Perl-Users-Digest
Perl-Users Digest, Issue: 9529 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jul 27 09:05:48 2006
Date: Thu, 27 Jul 2006 06:05:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 27 Jul 2006 Volume: 10 Number: 9529
Today's topics:
Re: Comparing values of multiple hash keys usenet@DavidFilmer.com
Re: Comparing values of multiple hash keys anno4000@radom.zrz.tu-berlin.de
Re: Comparing values of multiple hash keys <rvtol+news@isolution.nl>
Re: Count values in multi dimensional array <oli.meister@gmail.com>
Re: Count values in multi dimensional array <1usa@llenroc.ude.invalid>
DirHandle : the sort is different on Windows and on Lin thierry.torret@st.com
Re: garbage collector usenet@DavidFilmer.com
Re: garbage collector <Michael.F123@gmail.com>
How to find all files in a directory with a specific mt <schms@web.de>
Re: How to find all files in a directory with a specifi <mritty@gmail.com>
Re: How to find all files in a directory with a specifi <benmorrow@tiscali.co.uk>
Re: How to find all files in a directory with a specifi <schms@web.de>
Re: How to find all files in a directory with a specifi <schms@web.de>
Re: How to find all files in a directory with a specifi <1usa@llenroc.ude.invalid>
Re: How to find all files in a directory with a specifi <someone@example.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 27 Jul 2006 01:12:00 -0700
From: usenet@DavidFilmer.com
Subject: Re: Comparing values of multiple hash keys
Message-Id: <1153987920.861805.317940@m73g2000cwd.googlegroups.com>
Dr.Ruud wrote:
> usenet@DavidFilmer.com schreef:
> > push @results, "${count}::$word";
>
> OR-Variant:
> push(@results, "${count}::$word")
> if $count ;
> AND-Variant:
> push(@results, "${count}::$word")
> if $count == keys %keywords ;
The push statement in my code could never be executed unless the
keyword was found at least once in all data elements (as per the OP's
specification) so it would not be necessary to use conditional
expressions on the push as I coded it (interestingly, I sat in on a
great presentation at OSCON that Geoffrey Young gave just a few hours
ago on Devel::Cover which discussed how that module can help identify
redundant secondary checks).
However, the AND variant might not produce correct results. The OP
stipulated that all occurances of each search term be counted within
the data space, so $count could easily exceed keys(), and keys() might
be equal to $count even though the term was not found in all data terms
(because some terms matched more than once).
--
David Filmer (http://DavidFilmer.com)
------------------------------
Date: 27 Jul 2006 08:48:34 GMT
From: anno4000@radom.zrz.tu-berlin.de
Subject: Re: Comparing values of multiple hash keys
Message-Id: <4ircv2F504puU2@news.dfncis.de>
Ted Zlatanov <tzz@lifelogs.com> wrote in comp.lang.perl.misc:
> On 26 Jul 2006, tadmc@augustmail.com wrote:
>
> Jason <jwcarlton@gmail.com> wrote:
> >
> >> Originally, the script was simple. Take a keyword entered by a form,
> >> compare it to each value of an array (@data), and return any value
> >> containing the entry:
> >>
> >> $var = param('keyword');
> >> foreach $key (@data) {
> >> if ($key =~ /$var/i) { push (@founddata, $key); }
> >> }
> >>
> >>
> >> But now I'm trying to allow for multi-word phrases, which is a bit more
> >> complex. I couldn't find how others are doing it,
> >
> >
> > Do just what you are already doing (but a hash is more natural for
> > representing a set), but do it once for each term in the multi-word phrase.
> >
> > You then have N sets for your N-term phrase.
> >
> > Find the intersection of the N sets, and you have the answer.
> >
> > perldoc -q intersection
> >
> > How do I compute the difference of two arrays? How do I compute the
> > intersection of two arrays?
>
> The OP may want the union of the sets, based on his imprecise
> specifications :)
Maybe. The answer is still sufficient. The union isn't in the title,
but the faq explains how to compute it.
Anno
------------------------------
Date: Thu, 27 Jul 2006 14:42:13 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Comparing values of multiple hash keys
Message-Id: <eaajla.tg.1@news.isolution.nl>
usenet@DavidFilmer.com schreef:
> Dr.Ruud:
>> usenet@DavidFilmer.com:
>>> push @results, "${count}::$word";
>>
>> OR-Variant:
>> push(@results, "${count}::$word")
>> if $count ;
>> AND-Variant:
>> push(@results, "${count}::$word")
>> if $count == keys %keywords ;
>
> The push statement in my code could never be executed unless the
> keyword was found at least once in all data elements (as per the OP's
> specification) so it would not be necessary to use conditional
> expressions on the push as I coded it (interestingly, I sat in on a
> great presentation at OSCON that Geoffrey Young gave just a few hours
> ago on Devel::Cover which discussed how that module can help identify
> redundant secondary checks).
>
> However, the AND variant might not produce correct results. The OP
> stipulated that all occurances of each search term be counted within
> the data space, so $count could easily exceed keys(), and keys() might
> be equal to $count even though the term was not found in all data
> terms (because some terms matched more than once).
Oops yes, I overlooked the while.
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: 27 Jul 2006 00:24:01 -0700
From: "Oliver Meister" <oli.meister@gmail.com>
Subject: Re: Count values in multi dimensional array
Message-Id: <1153985041.367119.197350@b28g2000cwb.googlegroups.com>
[...]
>
> That only appears to work because all relevant hashes have the key
> "Exchange". That is a poor choice of test data. Add a few that
> don't have the key and try again. It will give you randomly wrong
> (or sometimes correct) results.
Thats true but the key "Exchange" will appear in all data sets anyway.
But it wouldn't be the right way to check the coun't from conceptional
point of view:
Both ways (grep and while loop) are certainly displaying the count but
we count the values in the key and not the number of values in the
forehand array.
For sure, I may haven't pointed that out clearly.
Anyway. I got it working by adopting A. Sinan Unurs suggestion [1].
[...]
>
> Anno
I've also posted the test - data [2].
Well, thank you Anno and Sinan Unur for you kind help (and patience ;-)
).
Regards
Oliver,
who is promissing to be more clear on a possible next topic post.
[1] Start copy/ pastable code
#!/usr/local/bin/perl
# Change shebang to your convenience
# Oliver Meister, Example for Posting
#
# Start
#
if (-e "data.xml")
{
print "### [OK] XML File exists !";
&f_initialisation; # Load Class, Module and File
# A. Sinan Unur
my @instruments = @{ $data->{InputList}->{Instrument} };
my $num_ex = grep { $_->{Exchange} } @instruments;
#
my $cnt = @instruments;
print "\nNumber of instruments sets, cnt: $cnt ";
print "\nNumber of instruments sets, num_ex: $num_ex \n";
print "\n\n\n\n more code here";
# [Debug] Display the parsed xml file
print "\n\n ################### \n\n";
print Dumper($data);
}
### FUNCTIONS
#
# Initialize perl mod for XML and Read XML File
#
sub f_initialisation
{
#
# Load Modules
#
use XML::Simple;
use Data::Dumper;
#
# create object
#
my $xml = new XML::Simple;
#
# Read XML File into array $data
#
$data = $xml->XMLin("data.xml");
return $data;
}
# EOF
[2] data.xml
<?xml version="1.0"?>
<ReportRequest xmlns="xxx">
<InputList>
<InputListAction>Replace</InputListAction>
<Name>nnn</Name>
<Instrument>
<IdentifierType>RIC</IdentifierType>
<Identifier>.SSHI</Identifier>
<Description>SPI TOTAL RETURN</Description>
<Exchange>SWX</Exchange>
</Instrument>
<Instrument>
<IdentifierType>RIC</IdentifierType>
<Identifier>AUD6MFSR=</Identifier>
<Description>Australian Dollar 6 month LIBOR</Description>
<Exchange>N/E</Exchange>
</Instrument>
<Instrument>
<IdentifierType>RIC</IdentifierType>
<Identifier>AUD7M=</Identifier>
<Description>Australian Dollar 7 month Forward</Description>
<Exchange>N/E</Exchange>
</Instrument>
<Instrument>
<IdentifierType>RIC</IdentifierType>
<Identifier>ZAR=</Identifier>
<Description>South African Rand Spot</Description>
<Exchange>N/E</Exchange>
</Instrument>
</InputList>
</ReportRequest>
------------------------------
Date: Thu, 27 Jul 2006 11:29:28 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Count values in multi dimensional array
Message-Id: <Xns980D4C4C550B2asu1cornelledu@127.0.0.1>
"Oliver Meister" <oli.meister@gmail.com> wrote in
news:1153985041.367119.197350@b28g2000cwb.googlegroups.com:
> Anyway. I got it working by adopting A. Sinan Unurs suggestion [1].
I am glad to be of help. See my other comments below.
...
> if (-e "data.xml")
> {
> print "### [OK] XML File exists !";
But, it might disappear by the time
> &f_initialisation; # Load Class, Module and File
this call is made. (Also, perldoc perlsub to find out the difference
between &f_initialisation and f_initialization(). Use the latter if you
do not specifically need the former. It is good style for the names of
subs to be action words.)
> sub f_initialisation
> {
> #
> # Load Modules
> #
> use XML::Simple;
> use Data::Dumper;
Are you under the impression that these modules are not loaded unless
f_initialisation is invoked?
use is a compile time statement, and the modules will be loaded no
whether or not this sub is invoked.
I find it far better, for clarity, to put all the use statements right
after the shebang line.
my $xml = new XML::Simple;
>
>
> #
> # Read XML File into array $data
> #
> $data = $xml->XMLin("data.xml");
>
> return $data;
Again, the fact that the this sub was invoked does not mean that the
file still exists by the time XMLin is called.
Finally, you seem not to be using strict and warnings. Not a good idea.
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
my ($xml_file) = @ARGV
or die "No filenames specified on the command line\n";
my $data = eval {
XMLin($xml_file);
};
$data or die "Could not parse '$xml_file':\n\t$@\n";
my @instruments = @{ $data->{InputList}->{Instrument} };
my $num_ex = grep exists $_->{Exchange}, @instruments;
printf "Number of instruments sets, cnt: %d\n", scalar @instruments;
print "Number of instruments sets, num_ex: $num_ex\n";
__END__
D:\UseNet\clpmisc> x1 data.xml
Number of instruments sets, cnt: 4
Number of instruments sets, num_ex: 4
Data file left intact for reference:
> [2] data.xml
>
> <?xml version="1.0"?>
> <ReportRequest xmlns="xxx">
> <InputList>
> <InputListAction>Replace</InputListAction>
> <Name>nnn</Name>
> <Instrument>
> <IdentifierType>RIC</IdentifierType>
> <Identifier>.SSHI</Identifier>
> <Description>SPI TOTAL RETURN</Description>
> <Exchange>SWX</Exchange>
> </Instrument>
> <Instrument>
> <IdentifierType>RIC</IdentifierType>
> <Identifier>AUD6MFSR=</Identifier>
> <Description>Australian Dollar 6 month LIBOR</Description>
> <Exchange>N/E</Exchange>
> </Instrument>
> <Instrument>
> <IdentifierType>RIC</IdentifierType>
> <Identifier>AUD7M=</Identifier>
> <Description>Australian Dollar 7 month Forward</Description>
> <Exchange>N/E</Exchange>
> </Instrument>
> <Instrument>
> <IdentifierType>RIC</IdentifierType>
> <Identifier>ZAR=</Identifier>
> <Description>South African Rand Spot</Description>
> <Exchange>N/E</Exchange>
> </Instrument>
> </InputList>
> </ReportRequest>
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: 27 Jul 2006 05:54:54 -0700
From: thierry.torret@st.com
Subject: DirHandle : the sort is different on Windows and on Linux
Message-Id: <1154004894.834494.179780@75g2000cwc.googlegroups.com>
I use a old Perl script using Dirhandle but the sort is different on
Windows and on Linux
$dh = new DirHandle($test) || return;
while ($OneFile = $dh->read()) {
In Windows the directory and files are sorted by name : it is OK
but on Linux there are sorted in order of directory
creation/modification date but we want it by name.
Is there any option to use in Dirhandle new or read to sort by named in
all system ?
I cannot modify too much the script therefore need to keep
while ($OneFile = $dh->read()) method
and not replace it by an array of directory name.
------------------------------
Date: 27 Jul 2006 01:27:52 -0700
From: usenet@DavidFilmer.com
Subject: Re: garbage collector
Message-Id: <1153988872.365504.259190@i42g2000cwa.googlegroups.com>
Michael.F123@gmail.com wrote:
> Do you have any ideea why tje garbage collector
> do not delete the objects afther the thread is finished?
Sure, I know exactly why. It's because your program has a logic error.
--
David Filmer (http://DavidFilmer.com)
------------------------------
Date: 27 Jul 2006 02:32:16 -0700
From: "Michael.F123@gmail.com" <Michael.F123@gmail.com>
Subject: Re: garbage collector
Message-Id: <1153992736.145563.186820@b28g2000cwb.googlegroups.com>
usenet@DavidFilmer.com wrote:
> Michael.F123@gmail.com wrote:
> > Do you have any ideea why tje garbage collector
> > do not delete the objects afther the thread is finished?
>
> Sure, I know exactly why. It's because your program has a logic error.
I think i use only 2 objects: the socket object the thread object. The
socket object i delete when the client close the connection and after i
close the connection the function end, so the thread ends also.
>
> --
> David Filmer (http://DavidFilmer.com)
------------------------------
Date: 27 Jul 2006 03:39:10 -0700
From: "schms" <schms@web.de>
Subject: How to find all files in a directory with a specific mtime
Message-Id: <1153996750.903661.135790@m73g2000cwd.googlegroups.com>
Hi
I have to write a perl program which finds all files in a directory
with mtime (modification time)
of "today - 8 days" ( e.g. today is: 27.7.06, -8 days is: 19.7.06, so
the file's modification
time should be 19.7.06)) and which end on *.ds.
As I am not expert in terms of perl, I found only the following poor
solution.
I was wondering, if anyone has a smarter solution:
use strict;
use File::Find;
use Time::Local;
use Date::Calc qw(Add_Delta_Days Today);
my $DIR = '/aaaaa/bbbb/ccccc';
my $DAYS = 8;
###
### get today
###
my ($YEAR, $MONTH, $DAY) = Today();
###
### get past date
###
my ($PYEAR, $PMONTH, $PDAY) = Add_Delta_Days($YEAR, $MONTH, $DAY,
-$DAYS );
my $epoche_pastdate_start = timelocal(0, 0, 0, $PDAY, $PMONTH-1,
$PYEAR-1900);
my $epoche_pastdate_end = timelocal(59, 59, 23, $PDAY, $PMONTH-1,
$PYEAR-1900);
find (\&my_func, $DIR);
sub my_func {
return unless -f;
return unless ( $File::Find::name =~ /.ds$/ );
my $epoche_mtime = (stat($File::Find::name))[9];
if ( $epoche_mtime >= $epoche_pastdate_start && $epoche_mtime
<= $epoche_pastdate_end ) {
system("ls -al $File::Find::name");
}
I thank you for your comments.
Stefan
------------------------------
Date: 27 Jul 2006 04:10:09 -0700
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: How to find all files in a directory with a specific mtime
Message-Id: <1153998609.452201.126030@i3g2000cwc.googlegroups.com>
schms wrote:
> Hi
>
> I have to write a perl program which finds all files in a directory
> with mtime (modification time)
>
> of "today - 8 days" ( e.g. today is: 27.7.06, -8 days is: 19.7.06, so
> the file's modification
>
> time should be 19.7.06)) and which end on *.ds.
>
> As I am not expert in terms of perl, I found only the following poor
> solution.
You have not at all explained what is "poor" about this solution. Is
it inefficient? Does it produce incorrect results? Is the code too
difficult to understand?
It's difficult to suggest an improvement when we don't know how the
current solution is lacking.
>
> I was wondering, if anyone has a smarter solution:
>
>
> use strict;
> use File::Find;
> use Time::Local;
> use Date::Calc qw(Add_Delta_Days Today);
>
> my $DIR = '/aaaaa/bbbb/ccccc';
> my $DAYS = 8;
>
> ###
> ### get today
> ###
>
> my ($YEAR, $MONTH, $DAY) = Today();
>
>
> ###
> ### get past date
> ###
>
> my ($PYEAR, $PMONTH, $PDAY) = Add_Delta_Days($YEAR, $MONTH, $DAY,
> -$DAYS );
>
>
> my $epoche_pastdate_start = timelocal(0, 0, 0, $PDAY, $PMONTH-1,
> $PYEAR-1900);
> my $epoche_pastdate_end = timelocal(59, 59, 23, $PDAY, $PMONTH-1,
> $PYEAR-1900);
There's no reason for any of this.
>
>
> find (\&my_func, $DIR);
>
> sub my_func {
>
> return unless -f;
>
> return unless ( $File::Find::name =~ /.ds$/ );
return unless -f and /\.ds$/;
>
> my $epoche_mtime = (stat($File::Find::name))[9];
>
> if ( $epoche_mtime >= $epoche_pastdate_start && $epoche_mtime
> <= $epoche_pastdate_end ) {
my $mtime_days = -M;
if ($mtime_days >=8 and $mtime_days < 9) {
>
> system("ls -al $File::Find::name");
print "$File::Find::name\n";
> }
>
Paul Lalli
------------------------------
Date: Thu, 27 Jul 2006 12:10:34 +0100
From: Ben Morrow <benmorrow@tiscali.co.uk>
Subject: Re: How to find all files in a directory with a specific mtime
Message-Id: <aijmp3-p4j.ln1@osiris.mauzo.dyndns.org>
Quoth "schms" <schms@web.de>:
> Hi
>
> I have to write a perl program which finds all files in a directory
> with mtime (modification time)
>
> of "today - 8 days" ( e.g. today is: 27.7.06, -8 days is: 19.7.06, so
> the file's modification
>
> time should be 19.7.06)) and which end on *.ds.
>
> As I am not expert in terms of perl, I found only the following poor
> solution.
>
> I was wondering, if anyone has a smarter solution:
>
>
> use strict;
Good... you also want
use warnings;
> use File::Find;
Do you need to recurse into directories (are there directories to
recurse into)? If not, something like
grep { <check timestamp> } glob '*.ds';
would probably be easier.
> use Time::Local;
> use Date::Calc qw(Add_Delta_Days Today);
>
> my $DIR = '/aaaaa/bbbb/ccccc';
> my $DAYS = 8;
>
> ###
> ### get today
> ###
>
> my ($YEAR, $MONTH, $DAY) = Today();
>
>
> ###
> ### get past date
> ###
>
> my ($PYEAR, $PMONTH, $PDAY) = Add_Delta_Days($YEAR, $MONTH, $DAY,
> -$DAYS );
>
>
> my $epoche_pastdate_start = timelocal(0, 0, 0, $PDAY, $PMONTH-1,
> $PYEAR-1900);
> my $epoche_pastdate_end = timelocal(59, 59, 23, $PDAY, $PMONTH-1,
> $PYEAR-1900);
I was going to suggest using -M, but it seems you specifically want just
the one day; and something like -M > 8 && -M <= 9 will be offset by the
current time-of-day.
> find (\&my_func, $DIR);
>
> sub my_func {
>
> return unless -f;
>
> return unless ( $File::Find::name =~ /.ds$/ );
You can just test $_ here, as above, whice is clearer; and you need to
escape the .
return unless /\.ds$/;
> my $epoche_mtime = (stat($File::Find::name))[9];
I would always use File::stat here, 'cos I don't like magic numbers.
> if ( $epoche_mtime >= $epoche_pastdate_start && $epoche_mtime
> <= $epoche_pastdate_end ) {
I found this test confusing: when I first read it, I thought it was
wrong. I would probably have $epoche_pastdate_end be a whole day later
(rather than one second shy of a day) and then test it with <.
> system("ls -al $File::Find::name");
I don't really like calling ls, but it's probably the easiest way to get
what you want. You don't need -a though: you're providing the list of
filenames.
I would probably build a list and use xargs, just for efficiency
(untested):
# outside the sub
my @names;
# here
push @names, $File::Find::name;
# after sub
open my $LS, '|-', xargs => ls => '-l'
or die "can't fork xargs: $!";
$, = $\ = "\n"; # or pass the -0 option to xargs and use \0
print $LS @names;
close $LS or die "xargs failed: $!";
> }
Ben
--
You poor take courage, you rich take care:
The Earth was made a common treasury for everyone to share
All things in common, all people one.
'We come in peace'---the order came to cut them down.[benmorrow@tiscali.co.uk]
------------------------------
Date: 27 Jul 2006 04:48:32 -0700
From: "schms" <schms@web.de>
Subject: Re: How to find all files in a directory with a specific mtime
Message-Id: <1154000912.130859.232270@m73g2000cwd.googlegroups.com>
Hi Paul
The code works but it is a bit complicated. So I thought, there must
be a smarter way to do it.
Your input:
(..)
if ($mtime_days >=8 and $mtime_days < 9) ..
(..)
My first idea was to use this solution, but it does not work as one get
also files with modification time of (today - 9).
Thanks.
Stefan
Paul Lalli schrieb:
> schms wrote:
> > Hi
> >
> > I have to write a perl program which finds all files in a directory
> > with mtime (modification time)
> >
> > of "today - 8 days" ( e.g. today is: 27.7.06, -8 days is: 19.7.06, so
> > the file's modification
> >
> > time should be 19.7.06)) and which end on *.ds.
> >
> > As I am not expert in terms of perl, I found only the following poor
> > solution.
>
> You have not at all explained what is "poor" about this solution. Is
> it inefficient? Does it produce incorrect results? Is the code too
> difficult to understand?
>
> It's difficult to suggest an improvement when we don't know how the
> current solution is lacking.
>
> >
> > I was wondering, if anyone has a smarter solution:
> >
> >
> > use strict;
> > use File::Find;
> > use Time::Local;
> > use Date::Calc qw(Add_Delta_Days Today);
> >
> > my $DIR = '/aaaaa/bbbb/ccccc';
> > my $DAYS = 8;
> >
> > ###
> > ### get today
> > ###
> >
> > my ($YEAR, $MONTH, $DAY) = Today();
> >
> >
> > ###
> > ### get past date
> > ###
> >
> > my ($PYEAR, $PMONTH, $PDAY) = Add_Delta_Days($YEAR, $MONTH, $DAY,
> > -$DAYS );
> >
> >
> > my $epoche_pastdate_start = timelocal(0, 0, 0, $PDAY, $PMONTH-1,
> > $PYEAR-1900);
> > my $epoche_pastdate_end = timelocal(59, 59, 23, $PDAY, $PMONTH-1,
> > $PYEAR-1900);
>
> There's no reason for any of this.
>
> >
> >
> > find (\&my_func, $DIR);
> >
> > sub my_func {
> >
> > return unless -f;
> >
> > return unless ( $File::Find::name =~ /.ds$/ );
>
> return unless -f and /\.ds$/;
>
> >
> > my $epoche_mtime = (stat($File::Find::name))[9];
> >
> > if ( $epoche_mtime >= $epoche_pastdate_start && $epoche_mtime
> > <= $epoche_pastdate_end ) {
>
> my $mtime_days = -M;
>
> if ($mtime_days >=8 and $mtime_days < 9) {
>
> >
> > system("ls -al $File::Find::name");
>
> print "$File::Find::name\n";
>
> > }
> >
>
> Paul Lalli
------------------------------
Date: 27 Jul 2006 04:56:49 -0700
From: "schms" <schms@web.de>
Subject: Re: How to find all files in a directory with a specific mtime
Message-Id: <1154001409.744482.281840@m73g2000cwd.googlegroups.com>
Ben Morrow schrieb:
> Quoth "schms" <schms@web.de>:
> > Hi
> >
> > I have to write a perl program which finds all files in a directory
> > with mtime (modification time)
> >
> > of "today - 8 days" ( e.g. today is: 27.7.06, -8 days is: 19.7.06, so
> > the file's modification
> >
> > time should be 19.7.06)) and which end on *.ds.
> >
> > As I am not expert in terms of perl, I found only the following poor
> > solution.
> >
> > I was wondering, if anyone has a smarter solution:
> >
> >
> > use strict;
>
> Good... you also want
>
> use warnings;
>
> > use File::Find;
>
> Do you need to recurse into directories (are there directories to
> recurse into)? If not, something like
>
> grep { <check timestamp> } glob '*.ds';
>
> would probably be easier.
I know, that File:Find is recursivly, but the directory does not have
any subdirectories.
> > use Time::Local;
> > use Date::Calc qw(Add_Delta_Days Today);
> >
> > my $DIR = '/aaaaa/bbbb/ccccc';
> > my $DAYS = 8;
> >
> > ###
> > ### get today
> > ###
> >
> > my ($YEAR, $MONTH, $DAY) = Today();
> >
> >
> > ###
> > ### get past date
> > ###
> >
> > my ($PYEAR, $PMONTH, $PDAY) = Add_Delta_Days($YEAR, $MONTH, $DAY,
> > -$DAYS );
> >
> >
> > my $epoche_pastdate_start = timelocal(0, 0, 0, $PDAY, $PMONTH-1,
> > $PYEAR-1900);
> > my $epoche_pastdate_end = timelocal(59, 59, 23, $PDAY, $PMONTH-1,
> > $PYEAR-1900);
>
> I was going to suggest using -M, but it seems you specifically want just
> the one day; and something like -M > 8 && -M <= 9 will be offset by the
> current time-of-day.
That is korrect (above).
>
> > find (\&my_func, $DIR);
> >
> > sub my_func {
> >
> > return unless -f;
> >
> > return unless ( $File::Find::name =~ /.ds$/ );
>
> You can just test $_ here, as above, whice is clearer; and you need to
> escape the .
>
> return unless /\.ds$/;
>
> > my $epoche_mtime = (stat($File::Find::name))[9];
>
> I would always use File::stat here, 'cos I don't like magic numbers.
>
> > if ( $epoche_mtime >= $epoche_pastdate_start && $epoche_mtime
> > <= $epoche_pastdate_end ) {
>
> I found this test confusing: when I first read it, I thought it was
> wrong. I would probably have $epoche_pastdate_end be a whole day later
> (rather than one second shy of a day) and then test it with <.
>
The system command is there just for debuging purposes....
> > system("ls -al $File::Find::name");
>
> I don't really like calling ls, but it's probably the easiest way to get
> what you want. You don't need -a though: you're providing the list of
> filenames.
>
> I would probably build a list and use xargs, just for efficiency
> (untested):
>
> # outside the sub
> my @names;
>
> # here
> push @names, $File::Find::name;
>
> # after sub
>
> open my $LS, '|-', xargs => ls => '-l'
> or die "can't fork xargs: $!";
>
> $, = $\ = "\n"; # or pass the -0 option to xargs and use \0
>
> print $LS @names;
>
> close $LS or die "xargs failed: $!";
>
> > }
>
> Ben
>
> --
> You poor take courage, you rich take care:
> The Earth was made a common treasury for everyone to share
> All things in common, all people one.
> 'We come in peace'---the order came to cut them down.[benmorrow@tiscali.co.uk]
------------------------------
Date: Thu, 27 Jul 2006 12:02:47 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: How to find all files in a directory with a specific mtime
Message-Id: <Xns980D51F293B53asu1cornelledu@127.0.0.1>
"schms" <schms@web.de> wrote in news:1154000912.130859.232270
@m73g2000cwd.googlegroups.com:
> Hi Paul
[ Please do not top-post, and remember to trim quoted material down
to only the parts that are actually relevant to your response. ]
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: Thu, 27 Jul 2006 13:04:35 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: How to find all files in a directory with a specific mtime
Message-Id: <DR2yg.133740$A8.74481@clgrps12>
schms wrote:
>
> I have to write a perl program which finds all files in a directory
> with mtime (modification time)
>
> of "today - 8 days" ( e.g. today is: 27.7.06, -8 days is: 19.7.06, so
> the file's modification
>
> time should be 19.7.06)) and which end on *.ds.
>
> As I am not expert in terms of perl, I found only the following poor
> solution.
>
> I was wondering, if anyone has a smarter solution:
>
>
> use strict;
> use File::Find;
>
> my $DIR = '/aaaaa/bbbb/ccccc';
> my $DAYS = 8;
>
>
> [ SNIP ]
>
> ###
> ### get past date
> ###
( my $epoche_pastdate = localtime time - $DAYS * 24 * 60 * 60 ) =~
s'\d+:\d+:\d+'\d+:\d+:\d+';
> find (\&my_func, $DIR);
>
> sub my_func {
>
> return unless -f;
>
> return unless ( $File::Find::name =~ /.ds$/ );
return unless -f and /\.ds\z/;
> my $epoche_mtime = (stat($File::Find::name))[9];
my $epoche_mtime = localtime( ( stat )[ 9 ] );
> if ( $epoche_mtime >= $epoche_pastdate_start && $epoche_mtime
> <= $epoche_pastdate_end ) {
if ( $epoche_mtime =~ $epoche_pastdate ) {
> system("ls -al $File::Find::name");
> }
John
--
use Perl;
program
fulfillment
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 9529
***************************************