[31675] in Perl-Users-Digest
Perl-Users Digest, Issue: 2938 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun May 9 16:09:26 2010
Date: Sun, 9 May 2010 13:09: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 Sun, 9 May 2010 Volume: 11 Number: 2938
Today's topics:
Re: find/copy most recent version of file? <geoff@invalid.invalid>
Re: find/copy most recent version of file? <jurgenex@hotmail.com>
Re: find/copy most recent version of file? <hjp-usenet2@hjp.at>
Re: find/copy most recent version of file? <hjp-usenet2@hjp.at>
Re: find/copy most recent version of file? <hjp-usenet2@hjp.at>
Re: find/copy most recent version of file? <tadmc@seesig.invalid>
Re: find/copy most recent version of file? <uri@StemSystems.com>
Re: find/copy most recent version of file? <geoff@invalid.invalid>
interesting perl question <robin1@cnsp.com>
Re: interesting perl question <ben@morrow.me.uk>
Re: interesting perl question <tadmc@seesig.invalid>
Re: Search pattern not terminated <derykus@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 09 May 2010 07:54:40 +0100
From: Geoff <geoff@invalid.invalid>
Subject: Re: find/copy most recent version of file?
Message-Id: <1imcu5125v1vbraar4cci1ongq6jpa404i@4ax.com>
On Sat, 08 May 2010 18:42:54 -0500, Tad McClellan
<tadmc@seesig.invalid> wrote:
>Geoff <geoff@invalid.invalid> wrote:
>
>> I meant to ask for an explanation of
>> what the sub wanted does.
>
>
>It determines whether the file under consideration is wanted or not.
Tad,
Even I with my well documented drawbacks understood that!
However I was hoping to understand better some of the the separate
lines in
sub wanted {
return unless /^video_c1_\d+\.mp4$/;
my $access_time = (stat($_))[9];
return if ($file_access{$_} and $access_time < $file_access{$_});
$file_full{$_} = $File::Find::name;
$file_access{$_} = $access_time;
}
I understand
return unless /^video_c1_\d+\.mp4$/;
my $access_time = (stat($_))[9];
but am not clear re
return if ($file_access{$_} and $access_time < $file_access{$_});
$file_full{$_} = $File::Find::name;
$file_access{$_} = $access_time;
e.g. what is $file_access, how does $File::Find::name work?
Cheers
Geoff
------------------------------
Date: Sun, 09 May 2010 00:55:50 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: find/copy most recent version of file?
Message-Id: <jspcu5hpn6c80huerv86h0638i3i8si620@4ax.com>
Geoff <geoff@invalid.invalid> wrote:
>On Sat, 08 May 2010 18:42:54 -0500, Tad McClellan
><tadmc@seesig.invalid> wrote:
>
>>Geoff <geoff@invalid.invalid> wrote:
>>
>>> I meant to ask for an explanation of
>>> what the sub wanted does.
>>
>>It determines whether the file under consideration is wanted or not.
>
>Even I with my well documented drawbacks understood that!
Well, if you meant to ask a different question then maybe you should
have asked a different question, ...
>sub wanted {
> return unless /^video_c1_\d+\.mp4$/;
> my $access_time = (stat($_))[9];
> return if ($file_access{$_} and $access_time < $file_access{$_});
> $file_full{$_} = $File::Find::name;
> $file_access{$_} = $access_time;
>}
>
>but am not clear re
>
> return if ($file_access{$_} and $access_time < $file_access{$_});
> $file_full{$_} = $File::Find::name;
> $file_access{$_} = $access_time;
>
>e.g. what is $file_access, how does $File::Find::name work?
... e.g. exactly these two questions:
$file_access is a hash. Mathematically you can think of hashes as a
mapping from string to scalar. They are a generalization of arrays which
map from integer (resp. natural numbers) to scalars.
How $File::Find::name works really is of absolutely no interest to
anyone unless you happen to be coding the File::Find module itself.
Actually you are explicitely supposed to not know it works because the
way it works could be changed at any time with any update of the module.
Instead you should be using the documented interface and trust, that
(unless there is a bug) the function will just do what it is supposed to
be doing as documented.
jue
------------------------------
Date: Sun, 9 May 2010 12:23:30 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: find/copy most recent version of file?
Message-Id: <slrnhud392.iio.hjp-usenet2@hrunkner.hjp.at>
On 2010-05-08 02:29, Jürgen Exner <jurgenex@hotmail.com> wrote:
> Martijn Lievaart <m@rtij.nl.invlalid> wrote:
>>On Wed, 05 May 2010 01:58:48 -0700, Jürgen Exner wrote:
>>> Geoff <geoff@invalid.invalid> wrote:
>>>>I have a series of files called c1_1.mp4 to c1_120.mp4 which are spread
>>>>over several folders.
>>>>I would like to be able to copy the most recent version of each file to
>>>>new folder.
>>>
>>> That is more of an algorithmic problem than a Perl problem. The easiest
>>> way: walk through all the possible source folders and copy each c1_*.mp4
>>> file into your target directory unless there exists a file with the same
>>> name in the target directory already and the existing file is newer than
>>> the file to be copied.
>>
>>That would mean a lot of unnecessary copying.
>
> True, but probably not as much as you would think.
>
> Assuming a completely random even distribution then if you have m
> instances each of n different files, then the first iteration will copy
> n files. For the second iteration on average you would only copy n/2
> files. And for each following iteration even less. Looks like tthere is
> probably something like m*log(n) involved, but I"m really too tired to
> figure it out in detail.
If each directory contains instance of the same files and the timestamps
are on a random even distribution, then each file will be copied
1 + 1/2 + 1/3 + ... + 1/m
times for m directories. Which looks vaguely like 1+log(m). So
O(n*log(m)) is a probably a good estimate for the average case on a
random even distribution. However, I think that this not a good
assumption. When there are a number of directories with roughly the same
files it seems likely that each directory contains a generation of these
files, i.e., all of them have roughly the same age. In this case, if you
walk the directories in oldest-to-newest order (which also is quite
likely on some file systems) you will wind up with the worst case, i.e.
O(m*n).
> But of course you are right, if you pre-compute the exact list then of
> course you can reduce the number of copies to exactly n.
Yup.
hp
------------------------------
Date: Sun, 9 May 2010 12:33:00 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: find/copy most recent version of file?
Message-Id: <slrnhud3qs.iio.hjp-usenet2@hrunkner.hjp.at>
On 2010-05-09 06:54, Geoff <geoff@invalid.invalid> wrote:
> On Sat, 08 May 2010 18:42:54 -0500, Tad McClellan
><tadmc@seesig.invalid> wrote:
>>Geoff <geoff@invalid.invalid> wrote:
>>> I meant to ask for an explanation of
>>> what the sub wanted does.
>>
>>
>>It determines whether the file under consideration is wanted or not.
>
> Even I with my well documented drawbacks understood that!
>
> However I was hoping to understand better some of the the separate
> lines in
The best way to understand something is to try to explain it to somebody
else. So try to write a posting which exactly explains what this program
does. If you don't understand something, try to figure it out by reading
the documentation (perldoc File::Find, perldoc perldata, perldoc
perlsyn, ...). If you still can't figure it out, write down your best
guess. Then post the whole description here and we will correct your
mistakes.
hp
------------------------------
Date: Sun, 9 May 2010 12:39:14 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: find/copy most recent version of file?
Message-Id: <slrnhud46i.iio.hjp-usenet2@hrunkner.hjp.at>
On 2010-05-09 00:28, Uri Guttman <uri@StemSystems.com> wrote:
>>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:
> JE> "Uri Guttman" <uri@StemSystems.com> wrote:
> >> my concern (docs not checked) is whether file::copy will handle a
> >> filename in the dest dir with missing dir parts. as in the dest is
> >> /foo and the file is found in bar/baz.text. will copy work with
> >> /foo/bar/baz.text is bar isn't there?
>
> JE> AFAIR it doesn't. However there is File::Path which will happily create
> JE> a directory path with a single command.
>
> oh i know about mkpath but the OP doesn't i am sure. and even then you
> need to get the proper path part if you want the tree to look sort of
> the same.
I don't think the OP wants that. As I understood him, he has multiple
instances of a file (say baz.text) scattered over several directories,
e.g.
/some/dir/baz.text
/some/other/dir/baz.text
/yet/another/dir/baz.text
He wants to find the newest instance and copy that to a target
directory, so if the target directory is named /target/, baz.text will
always be copied to /target/baz.text, regardless where it was found.
No mkpath necessary.
hp
------------------------------
Date: Sun, 09 May 2010 09:27:52 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: find/copy most recent version of file?
Message-Id: <slrnhudh9o.lk4.tadmc@tadbox.sbcglobal.net>
Geoff <geoff@invalid.invalid> wrote:
> I understand
>
> return unless /^video_c1_\d+\.mp4$/;
> my $access_time = (stat($_))[9];
>
> but am not clear re
>
> return if ($file_access{$_} and $access_time < $file_access{$_});
> $file_full{$_} = $File::Find::name;
> $file_access{$_} = $access_time;
>
Now there's a carefully composed question.
Congratulations on your progress! :-)
There is no $file_access (a scalar variable), there is however a
$file_access{$_}
(accessing one of the values in the hash named %file_access).
A hash is one of Perl's data structures, so you can read up
about it in the docs about Perl's data structures:
perldoc perldata
Hashes are unordered collections of scalar
values indexed by their associated string key.
The docs for File::Find (see "The wanted function" section) says
$_ is the current filename within that directory
$File::Find::name is the complete pathname to the file.
First, the Big Picture.
The wanted() subroutine implements the well-known "high watermark
algorithm".
You assume the 1st one is the "highest" (ie. the newest). The first
time you find say "blah.txt", you assume that it is the newest of
all the blah.txt files. Then each time you find another blah.txt
file you compare its access time to the saved access time. If it is
"newer", then you update what is recoreded as the newest version
of blah.txt.
Now on to the actual code:
return if ($file_access{$_} and $access_time < $file_access{$_});
$_ contains "blah.txt", so we have:
return if ($file_access{'blah.txt'}
and $access_time < $file_access{'blah.txt'});
That is, we return without updating anything if this blah.txt is older
than what we've recorded as the newest blah.txt.
If we get past this statement, then we have found a newer blah.txt,
so we need to record this new one as the newest:
$file_access{$_} = $access_time;
> e.g. what is $file_access, how does $File::Find::name work?
You do not need to know how $File::Find::name works.
You need to know what value $File::Find::name contains.
The docs for File::Find have already told you what it will contain,
"the complete pathname to the file"
So, $File::Find::name will contain something like
dir/someotherdir/blah.txt
which is stored
$file_full{$_} = $File::Find::name;
so that you can use it later after you have examined all of the blah.txt
files.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Sun, 09 May 2010 13:28:59 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: find/copy most recent version of file?
Message-Id: <87fx21kmqs.fsf@quad.sysarch.com>
>>>>> "PJH" == Peter J Holzer <hjp-usenet2@hjp.at> writes:
PJH> On 2010-05-09 00:28, Uri Guttman <uri@StemSystems.com> wrote:
>>>>>>> "JE" == Jürgen Exner <jurgenex@hotmail.com> writes:
JE> "Uri Guttman" <uri@StemSystems.com> wrote:
>> >> my concern (docs not checked) is whether file::copy will handle a
>> >> filename in the dest dir with missing dir parts. as in the dest is
>> >> /foo and the file is found in bar/baz.text. will copy work with
>> >> /foo/bar/baz.text is bar isn't there?
>>
JE> AFAIR it doesn't. However there is File::Path which will happily create
JE> a directory path with a single command.
>>
>> oh i know about mkpath but the OP doesn't i am sure. and even then you
>> need to get the proper path part if you want the tree to look sort of
>> the same.
PJH> He wants to find the newest instance and copy that to a target
PJH> directory, so if the target directory is named /target/, baz.text will
PJH> always be copied to /target/baz.text, regardless where it was found.
PJH> No mkpath necessary.
if you could find a logical statement from the OP which claims that
goal, more power to you! :)
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Sun, 09 May 2010 18:54:25 +0100
From: Geoff <geoff@invalid.invalid>
Subject: Re: find/copy most recent version of file?
Message-Id: <9ltdu5l9jmago5g6ds9hpbo6vdvmgiavsn@4ax.com>
On Sun, 09 May 2010 09:27:52 -0500, Tad McClellan
<tadmc@seesig.invalid> wrote:
>Geoff <geoff@invalid.invalid> wrote:
>
>> I understand
>>
>> return unless /^video_c1_\d+\.mp4$/;
>> my $access_time = (stat($_))[9];
>>
>> but am not clear re
>>
>> return if ($file_access{$_} and $access_time < $file_access{$_});
>> $file_full{$_} = $File::Find::name;
>> $file_access{$_} = $access_time;
>>
>
>
>Now there's a carefully composed question.
>
>Congratulations on your progress! :-)
>
>
>There is no $file_access (a scalar variable), there is however a
> $file_access{$_}
>(accessing one of the values in the hash named %file_access).
>
>A hash is one of Perl's data structures, so you can read up
>about it in the docs about Perl's data structures:
>
> perldoc perldata
>
> Hashes are unordered collections of scalar
> values indexed by their associated string key.
>
>The docs for File::Find (see "The wanted function" section) says
>
> $_ is the current filename within that directory
> $File::Find::name is the complete pathname to the file.
>
>
>First, the Big Picture.
>
>The wanted() subroutine implements the well-known "high watermark
>algorithm".
>
>You assume the 1st one is the "highest" (ie. the newest). The first
>time you find say "blah.txt", you assume that it is the newest of
>all the blah.txt files. Then each time you find another blah.txt
>file you compare its access time to the saved access time. If it is
>"newer", then you update what is recoreded as the newest version
>of blah.txt.
>
>Now on to the actual code:
>
> return if ($file_access{$_} and $access_time < $file_access{$_});
>
>$_ contains "blah.txt", so we have:
>
> return if ($file_access{'blah.txt'}
> and $access_time < $file_access{'blah.txt'});
>
>That is, we return without updating anything if this blah.txt is older
>than what we've recorded as the newest blah.txt.
>
>If we get past this statement, then we have found a newer blah.txt,
>so we need to record this new one as the newest:
>
> $file_access{$_} = $access_time;
>
>
>> e.g. what is $file_access, how does $File::Find::name work?
>
>
>You do not need to know how $File::Find::name works.
>
>You need to know what value $File::Find::name contains.
>
>The docs for File::Find have already told you what it will contain,
>"the complete pathname to the file"
>
>So, $File::Find::name will contain something like
>
> dir/someotherdir/blah.txt
>
>which is stored
>
> $file_full{$_} = $File::Find::name;
>
>so that you can use it later after you have examined all of the blah.txt
>files.
Many thanks Tad - extremely helpful.
Cheers
Geoff
------------------------------
Date: Sun, 9 May 2010 10:44:25 -0700 (PDT)
From: Robin <robin1@cnsp.com>
Subject: interesting perl question
Message-Id: <8355e429-f584-4b78-9856-7b67b05be2a8@g21g2000yqk.googlegroups.com>
how can I make a structure where you have a variable call sometjhing
in a package for exmaple print $a -> $v prints the variable $v in a
package called c.....the closest I could do was the
following....thanks everyone,
robin
package a;
$v = 1;
package main;
$a = bless \$z, "a";
print $a::v;
------------------------------
Date: Sun, 9 May 2010 18:54:26 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: interesting perl question
Message-Id: <if9jb7-f6n2.ln1@osiris.mauzo.dyndns.org>
Quoth Robin <robin1@cnsp.com>:
> how can I make a structure where you have a variable call sometjhing
> in a package for exmaple print $a -> $v prints the variable $v in a
> package called c.....the closest I could do was the
> following....thanks everyone,
This makes no sense. What are you trying to do?
> package a;
> $v = 1;
>
> package main;
> $a = bless \$z, "a";
> print $a::v;
You do realise that the '$a' in this line has nothing whatever to with
the '$a' in the line above, right?
Ben
------------------------------
Date: Sun, 09 May 2010 14:02:06 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: interesting perl question
Message-Id: <slrnhue1bu.m11.tadmc@tadbox.sbcglobal.net>
Robin <robin1@cnsp.com> wrote:
> how can I make a structure where you have a variable call
A variable is "data".
A function call is "code".
data cannot "call" anything only code can issue a function call.
> sometjhing
> in a package for exmaple print $a -> $v prints the variable $v in a
> package called c.....the closest I could do was the
> following
There IS NO package called c in your code.
> package a;
> $v = 1;
There are no functions in package a, so you cannot call anything
in package a, as there is nothing to call.
You should always enable strict in all of your Perl programs:
use strict;
so then, that last line needs to be:
our $v = 100;
(1 is often used for other things in Perl (eg true) so it is not
a good test value to use.
)
> package main;
> $a = bless \$z, "a";
Why do you include this line?
What do you think it does for you?
It is not needed, so you have a misunderstanding of the fundamentals
somewhere...
> print $a::v;
That prints the variable named $v in the package named a.
You do not need objects to access a variable that is declared
in another package.
--------------------
#!/usr/bin/perl
use warnings;
use strict;
package a;
our $v = 100;
package main;
print $a::v, "\n";
--------------------
Look Ma! No OO involved!
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Sun, 9 May 2010 04:19:48 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Search pattern not terminated
Message-Id: <2c3fd7fa-6b2f-42a4-99b0-41287b7d4f52@j36g2000prj.googlegroups.com>
On May 8, 8:27=A0pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> >>>>> "j" =3D=3D jidanni =A0<jida...@jidanni.org> writes:
>
> =A0 j> Is this really a search pattern, or do they need a better error me=
ssage
> =A0 j> about the typo of $++m for ++$m?
>
> =A0 j> $ perl -we '$r=3D$++m'
> =A0 j> Search pattern not terminated at -e line 1.
>
> that is parsed as $+ + m
>
> the m starts an m// regex but it never finds the terminator. hell, it
> should have even found the starting delimiter.
Search pattern not terminated at -e line 1 (#1)
(F) The lexer couldn't find the final delimiter of a // or m{}
construct. Remember that bracketing delimiters count nesting
level. Missing the leading $ from a variable $m may cause this
error. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^....
Looks like this latter scenario triggered the error since
the lexer didn't find a starting delimiter for m{}.
--
Charles DeRykus
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 V11 Issue 2938
***************************************