[29491] in Perl-Users-Digest
Perl-Users Digest, Issue: 735 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 9 16:09:43 2007
Date: Thu, 9 Aug 2007 13:09:12 -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, 9 Aug 2007 Volume: 11 Number: 735
Today's topics:
checking for newly added files <mislam@npspam.uiuc.edu>
Re: checking for newly added files <larry.grant.dc@gmail.com>
Re: checking for newly added files <larry.grant.dc@gmail.com>
Re: checking for newly added files <m@rtij.nl.invlalid>
Re: checking for newly added files <mislam@npspam.uiuc.edu>
Re: FAQ 7.26 How can I comment out a large block of per <larry.grant.dc@gmail.com>
Re: How to get values and ref types from caller package <el.dodgero@gmail.com>
Re: how to tranpose a huge text file <jiehuang001@gmail.com>
Re: more elegant way to say ($1, $2, $3, $4, ...)? <nobull67@gmail.com>
Re: more elegant way to say ($1, $2, $3, $4, ...)? <larry.grant.dc@gmail.com>
Re: more elegant way to say ($1, $2, $3, $4, ...)? <uri@stemsystems.com>
Re: more elegant way to say ($1, $2, $3, $4, ...)? <uri@stemsystems.com>
more inelegancies <larry.grant.dc@gmail.com>
Re: more inelegancies <mritty@gmail.com>
Re: more inelegancies <larry.grant.dc@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 09 Aug 2007 14:10:48 -0500
From: Sharif Islam <mislam@npspam.uiuc.edu>
Subject: checking for newly added files
Message-Id: <f9forq$ud3$1@news.ks.uiuc.edu>
I am copying (using scp -p) some text files from another machine and
processing them. I am not sure how to check when a new file
has been added to that particular directory. I will run the perl script
every night and I want it to look for new files, if a new file is there
I want to process it, otherwise ignore it.
#!/usr/bin/perl
use strict;
my $file;
# these are all the copied files
my @files = <*.txt>;
foreach $file (@files) {
my @tm = localtime((stat $file)[9]);
my $filedate=sprintf("%04d-%02d-%02d
%02d:%02d:%02d",$tm[5]+1900,$tm[4]+1,$tm[3],$tm[2],$tm[1],$tm[0]);
print "$filedate\n";
#here i will process the files.
}
------------------------------
Date: Thu, 09 Aug 2007 19:12:36 -0000
From: Larry <larry.grant.dc@gmail.com>
Subject: Re: checking for newly added files
Message-Id: <1186686756.170988.98480@m37g2000prh.googlegroups.com>
On Aug 9, 3:10 pm, Sharif Islam <mis...@npspam.uiuc.edu> wrote:
> I am copying (using scp -p) some text files from another machine and
> processing them. I am not sure how to check when a new file
> has been added to that particular directory. I will run the perl script
> every night and I want it to look for new files, if a new file is there
> I want to process it, otherwise ignore it.
>
> #!/usr/bin/perl
>
> use strict;
> my $file;
> # these are all the copied files
> my @files = <*.txt>;
>
> foreach $file (@files) {
> my @tm = localtime((stat $file)[9]);
> my $filedate=sprintf("%04d-%02d-%02d
> %02d:%02d:%02d",$tm[5]+1900,$tm[4]+1,$tm[3],$tm[2],$tm[1],$tm[0]);
> print "$filedate\n";
> #here i will process the files.
>
> }
Here's where Windows would come in handy :)
You could use the "archive" bit to keep track. I don't know if *nix
has anything similar.
------------------------------
Date: Thu, 09 Aug 2007 19:14:48 -0000
From: Larry <larry.grant.dc@gmail.com>
Subject: Re: checking for newly added files
Message-Id: <1186686888.278414.103290@m37g2000prh.googlegroups.com>
On Aug 9, 3:12 pm, Larry <larry.grant...@gmail.com> wrote:
> On Aug 9, 3:10 pm, Sharif Islam <mis...@npspam.uiuc.edu> wrote:
>
>
>
> > I am copying (using scp -p) some text files from another machine and
> > processing them. I am not sure how to check when a new file
> > has been added to that particular directory. I will run the perl script
> > every night and I want it to look for new files, if a new file is there
> > I want to process it, otherwise ignore it.
>
> > #!/usr/bin/perl
>
> > use strict;
> > my $file;
> > # these are all the copied files
> > my @files = <*.txt>;
>
> > foreach $file (@files) {
> > my @tm = localtime((stat $file)[9]);
> > my $filedate=sprintf("%04d-%02d-%02d
> > %02d:%02d:%02d",$tm[5]+1900,$tm[4]+1,$tm[3],$tm[2],$tm[1],$tm[0]);
> > print "$filedate\n";
> > #here i will process the files.
>
> > }
>
> Here's where Windows would come in handy :)
>
> You could use the "archive" bit to keep track. I don't know if *nix
> has anything similar.
Otherwise, after processing, write a file with the timestamp of the
most recent file that has been processed. Before processing, check
that file, and skip any file with a timestamp <= to that.
------------------------------
Date: Thu, 9 Aug 2007 21:41:13 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: checking for newly added files
Message-Id: <pan.2007.08.09.19.41.13@rtij.nl.invlalid>
On Thu, 09 Aug 2007 14:10:48 -0500, Sharif Islam wrote:
> I am copying (using scp -p) some text files from another machine and
> processing them. I am not sure how to check when a new file has been
> added to that particular directory. I will run the perl script every
> night and I want it to look for new files, if a new file is there I want
> to process it, otherwise ignore it.
If you are on un*x (or Windows with Cygwin) use rsync.
M4
------------------------------
Date: Thu, 09 Aug 2007 14:45:45 -0500
From: Sharif Islam <mislam@npspam.uiuc.edu>
Subject: Re: checking for newly added files
Message-Id: <f9fqt9$v9b$1@news.ks.uiuc.edu>
Martijn Lievaart wrote:
> On Thu, 09 Aug 2007 14:10:48 -0500, Sharif Islam wrote:
>
>> I am copying (using scp -p) some text files from another machine and
>> processing them. I am not sure how to check when a new file has been
>> added to that particular directory. I will run the perl script every
>> night and I want it to look for new files, if a new file is there I want
>> to process it, otherwise ignore it.
>
> If you are on un*x (or Windows with Cygwin) use rsync.
>
> M4
aha! yes, forgot about rsync. Thanks.
--sharif
------------------------------
Date: Thu, 09 Aug 2007 18:11:39 -0000
From: Larry <larry.grant.dc@gmail.com>
Subject: Re: FAQ 7.26 How can I comment out a large block of perl code?
Message-Id: <1186683099.830439.296440@57g2000hsv.googlegroups.com>
On Aug 7, 3:03 pm, PerlFAQ Server <br...@stonehenge.com> wrote:
> 7.26: How can I comment out a large block of perl code?
>
> You can use embedded POD to discard it. Enclose the blocks you want to
> comment out in POD markers. The <=begin> directive marks a section for a
> specific formatter. Use the "comment" format, which no formatter should
> claim to understand (by policy). Mark the end of the block with <=end>.
>
> # program is here
>
> =begin comment
>
> all of this stuff
>
> here will be ignored
> by everyone
>
> =end comment
>
> =cut
>
> # program continues
>
> The pod directives cannot go just anywhere. You must put a pod directive
> where the parser is expecting a new statement, not just in the middle of
> an expression or some other arbitrary grammar production.
>
> See perlpod for more details.
Or... if you are not really using POD in your script, you can do the
easier:
# program is here
=pod
all of this stuff
here will be ignored
by everyone
=cut
# program continues
------------------------------
Date: Thu, 09 Aug 2007 18:17:34 -0000
From: Dodger <el.dodgero@gmail.com>
Subject: Re: How to get values and ref types from caller package
Message-Id: <1186683454.842575.293800@q3g2000prf.googlegroups.com>
On Aug 7, 6:37 pm, mer...@stonehenge.com (Randal L. Schwartz) wrote:
> >>>>> "Dodger" == Dodger <el.dodg...@gmail.com> writes:
>
> Dodger> One of the other problems is that some of the coders have decided to
> Dodger> go ahead and call this thing numberous times even when they don't need
> Dodger> to. Rather than try and hunt down every instance of someone calling
> Dodger> for Foo when they already have a Foo object, i wanted to be able to
> Dodger> look into the symbol table for the caller, find all the variables
> Dodger> there, and check to see if any of the scalars are, in fact, already a
> Dodger> Foo, and, if so, just to return that and do nothing else. This would
> Dodger> bypass the need to check to make sure it's the right *kind* of Foo
> Dodger> because the first time a Foo was grabbed it would be and the
> Dodger> environment doesn't change at runtime. It's safe to trust the former
> Dodger> Foo is the right kind of Foo.
>
> That seems like the hard way. Why don't you just cache the result from
> Foo->new, using some sort of aspect programming (replace ->new with something
> that watches the inputs and return values from the real ->new)?
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
> <mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
> See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
>
> --
> Posted via a free Usenet account fromhttp://www.teranews.com
Out of curiosity, though... even if this isn't the right way to go,
how can I do this if some time it is the right (or only) solution? Is
there a way to peek right into and get the variables out of the
calling package, maybe even manipulate them?
--
Dodger
------------------------------
Date: Thu, 09 Aug 2007 19:32:33 -0000
From: Jie <jiehuang001@gmail.com>
Subject: Re: how to tranpose a huge text file
Message-Id: <1186687953.005301.137240@e16g2000pri.googlegroups.com>
Hi, Thank you so much for all the responses.
First, here is a sample dataset, but the real one is much bigger, with
1,000 columns instead of 14.
http://www.humanbee.com/BIG.txt
I could think of two ways to transpose this file.
Option1: write a line as a column and append, something like below
open IN, "<big_file.txt";
open OUT, ">transposed_file.txt";
while (IN) {
append ??????????????
}
Option 2: generate a huge 2-dimentional array and write it out in the
other way
$row=0;
while (IN) {
$big_ARRAY[$row][] = split(/ /);
}
foreach $row (0 ..$row) {
foreach $column (0 ..$#big_ARRAY) {
print OUT$big_ARRAY[$column][$row];
}
}
But I really doubt that either will work. So, can someone please throw
some idea and hopefully code here?!
Thank you!!
Jie
------------------------------
Date: Thu, 09 Aug 2007 18:29:13 -0000
From: Brian McCauley <nobull67@gmail.com>
Subject: Re: more elegant way to say ($1, $2, $3, $4, ...)?
Message-Id: <1186684153.441836.4630@57g2000hsv.googlegroups.com>
On Aug 9, 7:04 pm, Larry <larry.grant...@gmail.com> wrote:
> I'm using a /g regex in a while loop to capture parenthesized matches
> to meaningful variable names like this:
>
> while (/ (...) ... (...) ... (...)/g) {
> my ($foo, $bar, $baz) = ($1, $2, $3);
> ...
>
> }
>
> The ($1, $2, $3) part seems inelegant ... is there a more elegant way?
Not that I know of in Perl5 - and believe me I've looked.
You could write a function that returns them
sub matches { no strict 'refs'; map $$_ , 1 .. $#- }
while (/ (...) ... (...) ... (...)/g) {
my ($foo, $bar, $baz) = matches;
}
But this is hardly more elegant.
------------------------------
Date: Thu, 09 Aug 2007 18:52:55 -0000
From: Larry <larry.grant.dc@gmail.com>
Subject: Re: more elegant way to say ($1, $2, $3, $4, ...)?
Message-Id: <1186685575.597569.49770@e16g2000pri.googlegroups.com>
On Aug 9, 2:29 pm, Brian McCauley <nobul...@gmail.com> wrote:
> On Aug 9, 7:04 pm, Larry <larry.grant...@gmail.com> wrote:
>
> > I'm using a /g regex in a while loop to capture parenthesized matches
> > to meaningful variable names like this:
>
> > while (/ (...) ... (...) ... (...)/g) {
> > my ($foo, $bar, $baz) = ($1, $2, $3);
> > ...
>
> > }
>
> > The ($1, $2, $3) part seems inelegant ... is there a more elegant way?
>
> Not that I know of in Perl5 - and believe me I've looked.
>
> You could write a function that returns them
>
> sub matches { no strict 'refs'; map $$_ , 1 .. $#- }
>
> while (/ (...) ... (...) ... (...)/g) {
> my ($foo, $bar, $baz) = matches;
>
> }
>
> But this is hardly more elegant.
Not elegant?! It's awesome! Thanks!
BTW, I just learned 2 new things:
-- map can take an expr as the first param, not just a block (had to
look that up to see what was going on exactly!)
-- that there is a variable called @- and what it does
Thanks!
------------------------------
Date: Thu, 09 Aug 2007 19:08:14 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: more elegant way to say ($1, $2, $3, $4, ...)?
Message-Id: <x7d4xwle5a.fsf@mail.sysarch.com>
>>>>> "L" == Larry <larry.grant.dc@gmail.com> writes:
L> I'm using a /g regex in a while loop to capture parenthesized matches
L> to meaningful variable names like this:
L> while (/ (...) ... (...) ... (...)/g) {
L> my ($foo, $bar, $baz) = ($1, $2, $3);
L> ...
L> }
L> The ($1, $2, $3) part seems inelegant ... is there a more elegant way?
you can look at using @+, @- to get the strings via substr. a map call
on 0 .. $#+ will do it but it is ugly too
perldoc perlvar says this:
$1 is the same as "substr($var, $-[1], $+[1] - $-[1])"
so this should work (untested):
my ($foo, $bar, $baz) =
map substr($var, $-[$_], $+[$_] - $-[$_]), 0 .. $#+ ;
and that map stuff could be put into a sub to clean it up. just pass in
$var and the @+ and @- globals should still be set. something like this:
sub matches {
map substr($_[0], $-[$_], $+[$_] - $-[$_]), 0 .. $#+ ;
}
my ($foo, $bar, $baz) = matches( $var ) ;
but i would just stick with the assignment of $1, $2 ... as it is the
cleanest.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Thu, 09 Aug 2007 19:10:17 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: more elegant way to say ($1, $2, $3, $4, ...)?
Message-Id: <x78x8kle1v.fsf@mail.sysarch.com>
>>>>> "L" == Larry <larry.grant.dc@gmail.com> writes:
L> On Aug 9, 2:29 pm, Brian McCauley <nobul...@gmail.com> wrote:
>> On Aug 9, 7:04 pm, Larry <larry.grant...@gmail.com> wrote:
>> You could write a function that returns them
>>
>> sub matches { no strict 'refs'; map $$_ , 1 .. $#- }
>>
>> while (/ (...) ... (...) ... (...)/g) {
>> my ($foo, $bar, $baz) = matches;
>>
>> }
>>
>> But this is hardly more elegant.
L> Not elegant?! It's awesome! Thanks!
it is not elegant as it uses symrefs which is evil. see my other post
for a solution without symrefs.
L> -- that there is a variable called @- and what it does
and see @+ and how perlvar says to use them. my other post shows a full
example without symrefs.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Thu, 09 Aug 2007 18:59:36 -0000
From: Larry <larry.grant.dc@gmail.com>
Subject: more inelegancies
Message-Id: <1186685976.680652.162430@g12g2000prg.googlegroups.com>
While we are on the subject of inelegancies ... what if I want to
assign the elements of @zam to scalars, but I don't care about
$zam[2]? I'd like to do:
my ($foo, $bar, undef, $baz) = @zam;
but that won't compile, so I'm forced to do:
my ($foo, $bar, $baz);
($foo, $bar, undef, $baz) = @zam;
Any better way?
And furthermore....
What if $foo and $baz are already my'ed... I'd like to do:
($foo, my $bar, $baz) = @wham;
but Perl doesn't like that either, so I have to do:
my $bar;
($foo, $bar, $baz) = @wham;
------------------------------
Date: Thu, 09 Aug 2007 12:35:54 -0700
From: Paul Lalli <mritty@gmail.com>
Subject: Re: more inelegancies
Message-Id: <1186688154.537550.134860@q3g2000prf.googlegroups.com>
On Aug 9, 2:59 pm, Larry <larry.grant...@gmail.com> wrote:
> While we are on the subject of inelegancies ... what if I want to
> assign the elements of @zam to scalars, but I don't care about
> $zam[2]? I'd like to do:
>
> my ($foo, $bar, undef, $baz) = @zam;
>
> but that won't compile,
Says who?
$ perl -le'
my @zam = qw/alpha beta gamma delta/;
my ($foo, $bar, undef, $baz) = @zam;
print for $foo, $bar, $baz;
'
alpha
beta
delta
> Any better way?
Array slices.
$ perl -le'
my @zam = qw/alpha beta gamma delta/;
my ($foo, $bar, $baz) = @zam[0,1,3];
print for $foo, $bar, $baz;
'
alpha
beta
delta
> And furthermore....
>
> What if $foo and $baz are already my'ed... I'd like to do:
>
> ($foo, my $bar, $baz) = @wham;
>
> but Perl doesn't like that either
Exactly what version of Perl are you running? This, again, works just
fine for me.
$ perl -le'
my @zam = qw/alpha beta gamma delta/;
my ($foo, $baz);
($foo, my $bar, $baz) = @zam[0,1,3];
print for $foo, $bar, $baz;
'
alpha
beta
delta
And indeed, B::Deparse even shows us that it's doing exactly what we
mean (that the second 'my' applies only to $bar, not $baz:
$ perl -MO=Deparse,-p -le'
my @zam = qw/alpha beta gamma delta/;
my ($foo, $baz);
($foo, my $bar, $baz) = @zam[0,1,3];
print for $foo, $bar, $baz;
'
BEGIN { $/ = "\n"; $\ = "\n"; }
(my(@zam) = ('alpha', 'beta', 'gamma', 'delta'));
my($foo, $baz);
(($foo, my($bar), $baz) = @zam[0, 1, 3]);
;
foreach $_ (($foo, $bar, $baz)) {
print($_);
}
-e syntax OK
Paul Lalli
------------------------------
Date: Thu, 09 Aug 2007 19:57:03 -0000
From: Larry <larry.grant.dc@gmail.com>
Subject: Re: more inelegancies
Message-Id: <1186689423.305785.192040@q3g2000prf.googlegroups.com>
On Aug 9, 3:35 pm, Paul Lalli <mri...@gmail.com> wrote:
> Exactly what version of Perl are you running? This, again, works just
> fine for me.
??!!! They are working for me too! I don't know what I was
thinking... maybe they used to not work in some older version of Perl
and I never checked.... ??
------------------------------
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 V11 Issue 735
**************************************