[25114] in Perl-Users-Digest
Perl-Users Digest, Issue: 7364 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 5 14:05:43 2004
Date: Fri, 5 Nov 2004 11:05:10 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 5 Nov 2004 Volume: 10 Number: 7364
Today's topics:
Re: Array -> Reference to Hash of Arrays <tadmc@augustmail.com>
Re: Array -> Reference to Hash of Arrays <nobull@mail.com>
Re: Best way to handle using a report template for non- <john_pataki@yahoo.com>
Re: Cgi/Perl script for multiple file upload/attachment (Avin Patel)
Re: Cgi/Perl script for multiple file upload/attachment <noreply@gunnar.cc>
Re: Cgi/Perl script for multiple file upload/attachment <nobull@mail.com>
Re: Check POP3 E-mail <1usa@llenroc.ude.invalid>
Re: Check POP3 E-mail <nobull@mail.com>
Re: DEFINEs in Perl? <jgibson@mail.arc.nasa.gov>
Re: DEFINEs in Perl? <usenet_05_08_2004@stuartmoore.org.uk>
Re: EOF issue <onedoesnot@needto.know>
Re: EOF issue ctcgag@hotmail.com
FAQ 8.25: How can I capture STDERR from an external com <comdog@panix.com>
Re: Finding consecutive lines <tadmc@augustmail.com>
Re: Finding consecutive lines <bmb@ginger.libs.uga.edu>
Re: Finding consecutive lines <dwall@fastmail.fm>
Re: Finding consecutive lines ctcgag@hotmail.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 5 Nov 2004 09:58:40 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Array -> Reference to Hash of Arrays
Message-Id: <slrncon8pg.gvj.tadmc@magna.augustmail.com>
Tom <sudhirx@gmail.com> wrote:
> How to create/add/delete/print an "Array which Refers to Hash of
> Arrays"
That would commonly be called a LoHoL (list of hashes of lists),
but more precisely called "array of references to hashes of array
references" I guess.
Have you seen the references tutorial yet?
perldoc perlreftut
Or the other docs about complex data structures?
perldoc perllol
perldoc perldsc
> Let me know if this needs more explanation !
Let us know if perlreftut doesn't do it for you.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 05 Nov 2004 17:17:27 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Array -> Reference to Hash of Arrays
Message-Id: <cmgcac$5ns$1@sun3.bham.ac.uk>
Tom wrote:
> Hope my subject is right !
> Well, Here is what 'm trying to fight with
>
> How to create/add/delete/print an "Array which Refers to Hash of
> Arrays"
>
> Array - List of test cases (Not-Unique)
> Hash - Key: Test Case Iteration # (Unique)
> Value: <More than one value> i.e Array
If "Test Case Iteration #" is simply a sequential integer then it would
be more natural to use an array rather than a hash for your second level
structure.
If the "<More than one value>" are disimilar things like "duration",
"memory usage", "number of rabits pulled from hat" then it is unnatural
to put them in an array and would be more natural to use a hash.
Only if "<More than one value>" is a list of like values does an array
seem the natural choice. e.g. "weight of first rabit","weight of second
rabit" ... "weight of last rabit".
Anyhow, as others have said, if you find something in the documentation
unclear then please tell us precicely what so that we can have a chance
to improve the documentation.
------------------------------
Date: 5 Nov 2004 09:49:31 -0800
From: "Johnny Google" <john_pataki@yahoo.com>
Subject: Re: Best way to handle using a report template for non-programmers..
Message-Id: <1099676971.769530.126760@f14g2000cwb.googlegroups.com>
Ok - perhaps I am missing something here -- what I was asking was a way
to handle the actual template ... i am not quite sure how this code
would help with generating formatted ascii reports with a template?
What is a sample Template file in your case?
John
------------------------------
Date: 5 Nov 2004 08:10:54 -0800
From: avinpatel@gmail.com (Avin Patel)
Subject: Re: Cgi/Perl script for multiple file upload/attachment in web form
Message-Id: <1655d2a1.0411050810.2981a21@posting.google.com>
Hi Wana,
Thanks for the answer.
What is the cgi.pm? Is it normal cgi-perl. Do I need to install
anything on my web server to use cgi.pm based perl script?
Thanks,
Avin Patel
wana <ioneabu@yahoo.com> wrote in message news:<10ol845o7gi9j4a@news.supernews.com>...
> Avin Patel wrote:
>
> > Hi,
> > I am looking for perl/php script to allow multiple files can be
> > uploaded/attached in webform.
> >
> > But I don't like the multiple input boxes using "<input type="file"
> > size="40" maxlength="40" name="filename[]">" html tag. As it will
> > limit the no. of files can be uploaded(only as many of this boxes I
> > write in form & doesn't look good in form also).
> >
> > I ma looking for multiple file upload in web form using html tags
> > select or textarea.
> >
> > Can any one suggest where this script can be found, I googled..., but
> > no luck over there.
> >
> > Thanks,
> > Avin Patel
>
> Try amazon.com. There is a great book on the subject in general: 'CGI.pm'
> by Dr. Lincoln Stein. It is really a lot of fun using CGI.pm to do just
> what you are trying to do. Good luck!
>
> wana
------------------------------
Date: Fri, 05 Nov 2004 17:21:40 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Cgi/Perl script for multiple file upload/attachment in web form
Message-Id: <2v1ns4F2fofb8U1@uni-berlin.de>
Avin Patel wrote:
> I am looking for perl/php script to allow multiple files can be
> uploaded/attached in webform.
This group is for discussing Perl programming matters. If you are
looking for a ready-to-go script, try sites like
http://cgi.resourceindex.com/Programs_and_Scripts/Perl/ or use Google.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Fri, 05 Nov 2004 17:49:16 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Cgi/Perl script for multiple file upload/attachment in web form
Message-Id: <cmge62$6o9$1@sun3.bham.ac.uk>
Avin Patel rudely spits TOFU in Wana's face:
>>Avin Patel wrote:
>>>I am looking for perl/php script to allow multiple files can be
>>>uploaded/attached in webform.
>>Try amazon.com. There is a great book on the subject in general: 'CGI.pm'
>>by Dr. Lincoln Stein. It is really a lot of fun using CGI.pm to do just
>>what you are trying to do.
> What is the cgi.pm?
A mistyping of CGI.pm. Case is significant in Perl.
> Is it normal cgi-perl.
Yes, any other way of doing CGI in Perl is abnornal. (Not that there
aren't other ways but CGI.pm is definitely the 'normal' one).
> Do I need to install anything on my web server to use cgi.pm based perl script?
A moderately recent version of Perl. Any from this centurary should be
OK IIRC.
------------------------------
Date: 5 Nov 2004 17:35:55 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Check POP3 E-mail
Message-Id: <Xns9598802954588asu1cornelledu@132.236.56.8>
krakle@visto.com (krakle) wrote in
news:237aaff8.0411050643.56e045a0@posting.google.com:
> arguement. mod_perl scripts stay in memory. Unless you reset the
> values of a variable at the beginning of the script or before that
> variable is used it may contain the previous users data...
Your statement is full of half truths. If you don't write 'good' programs,
then bad things surely mya happen. However, I have quite a few CGI scripts
that are being run under mod_perl, and I have yet to run into such an
issue.
Sinan.
------------------------------
Date: Fri, 05 Nov 2004 18:21:17 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: Check POP3 E-mail
Message-Id: <cmgg24$7ir$1@sun3.bham.ac.uk>
Gunnar Hjalmarsson wrote:
> Global variables retain their values in mod_perl, but I don't think
> that's true for lexically scoped variables.
Top level lexically scoped variables in modules do retain their values
under mod_perl as one would expect.
Personally I dislike the use of the term "global" to refer to package
variables since for some purposes top-level lexicals should also be
considered global.
CGI scripts running under registry will be wrapped in a subroutine
wrapper so once this has happened they don't have any top level
lexically scoped variables. If you did have any and were relying on
them behaving as global variables (i.e. being shared by all subroutines)
then you should replace them with package variables. Having done this
you will need to explicitly undefine them if you want to avoid them
retaining their values. It is best to do this using local()[1].
You must of course remember that the retained value is not session aware.
[1] There are people who disagree with me about this. This is becasue
they've heard that using local() is bad. This in turn is because many
people have in the past used local() where they really would be better
off with the semantics of my(). This message has then taken on
religious proportions such that people believe local() is bad even when
they actually want the semantics of local() and not those of my().
------------------------------
Date: Fri, 05 Nov 2004 09:22:43 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: DEFINEs in Perl?
Message-Id: <051120040922433403%jgibson@mail.arc.nasa.gov>
In article <1099668625.475797@nntp.acecape.com>, daniel kaplan
<nospam@nospam.com> wrote:
> "Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
> news:cmfjor$cfq$1@mamenchi.zrz.TU-Berlin.DE...
[ Anno's hash-based dispatch table example snipped]
>
> ....i see how you say to do it, but have to wonder, when using a KEY, isn't
> there still a string compare happening behind the scenes? and when it
> comes to that, am no assembly code expert but, comparing a 1 to a 2 is far
> quicker than comparing "cat" to "catch"
>
There is no string comparison going on. The key string is converted
('hashed') to a value that is used to find the corresponding entry. I
am not familiar with Perl internals, but it is probably a numerical
value and the lookup can occur in a fixed amount of time. That is why
hashes are efficient. Compare the hash-lookup with the successive
if-elsif-elsif statements, each of which requires a separate numerical
comparison. The hash lookup scales to any number of values, while the
if statement does not.
------------------------------
Date: Fri, 05 Nov 2004 18:16:59 +0000
From: Stuart Moore <usenet_05_08_2004@stuartmoore.org.uk>
Subject: Re: DEFINEs in Perl?
Message-Id: <cmgg2q$prd$1@gemini.csx.cam.ac.uk>
Jim Gibson wrote:
> In article <1099668625.475797@nntp.acecape.com>, daniel kaplan
> <nospam@nospam.com> wrote:
>
>
>>"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> wrote in message
>>news:cmfjor$cfq$1@mamenchi.zrz.TU-Berlin.DE...
>
>
> [ Anno's hash-based dispatch table example snipped]
>
>>....i see how you say to do it, but have to wonder, when using a KEY, isn't
>>there still a string compare happening behind the scenes? and when it
>>comes to that, am no assembly code expert but, comparing a 1 to a 2 is far
>>quicker than comparing "cat" to "catch"
>>
>
>
> There is no string comparison going on. The key string is converted
> ('hashed') to a value that is used to find the corresponding entry. I
> am not familiar with Perl internals, but it is probably a numerical
> value and the lookup can occur in a fixed amount of time. That is why
> hashes are efficient. Compare the hash-lookup with the successive
> if-elsif-elsif statements, each of which requires a separate numerical
> comparison. The hash lookup scales to any number of values, while the
> if statement does not.
Although presumably a lookup in an array would be quicker if the
constants could be suitably chosen to be 1..$n
Stuart
------------------------------
Date: Fri, 5 Nov 2004 16:40:27 -0000
From: "IanW" <onedoesnot@needto.know>
Subject: Re: EOF issue
Message-Id: <cmgb6b$111$1@blackmamba.itd.rl.ac.uk>
"Thomas Kratz" <ThomasKratz@REMOVEwebCAPS.de> wrote in message
news:418b939f$0$17108$bb690d87@news.main-rheiner.de...
[..]
I don't understand the program logic here..
> local $/ = '=' x 15 . "\n";
ok, so changing the input record separater to '=' x 15 . "\n" (referred to
as "=15" hereafter for convenience), effectively specifying how you're going
to split the file (I actually need the =15 when I output the chunks after
processing them, but I can add that back in when printing out to file).
> while ( my $chunk = <FH> ) {
that's going to slurp in all text up to and including "Line 3" in my
original example on the first pass of the loop.
> chomp($chunk);
removes any newline characters from the very end of $chunk and I see when I
comment this line out in testing, it causes the following =15 to be slurped
into the chunk aswell, but I've not yet figured out why that newline
character makes a difference since you've already told Perl you're splitting
the file up by =15?
> next unless $chunk =~ /\w/;
> # process chunk
So this says that we're going to process $chunk if it has an alphanumeric
character in it, which it would if the first chunk has been successfuly
slurped int?
IanW
------------------------------
Date: 05 Nov 2004 17:46:41 GMT
From: ctcgag@hotmail.com
Subject: Re: EOF issue
Message-Id: <20041105124641.463$iG@newsreader.com>
"IanW" <onedoesnot@needto.know> wrote:
> "Thomas Kratz" <ThomasKratz@REMOVEwebCAPS.de> wrote in message
> news:418b939f$0$17108$bb690d87@news.main-rheiner.de...
> [..]
>
> I don't understand the program logic here..
>
> > local $/ = '=' x 15 . "\n";
>
> ok, so changing the input record separater to '=' x 15 . "\n" (referred
> to as "=15" hereafter for convenience), effectively specifying how you're
> going to split the file (I actually need the =15 when I output the chunks
> after processing them, but I can add that back in when printing out to
> file).
>
> > while ( my $chunk = <FH> ) {
>
> that's going to slurp in all text up to and including "Line 3" in my
> original example on the first pass of the loop.
Up to and including the first Line 3, and also including the =15 after it.
> > chomp($chunk);
>
> removes any newline characters from the very end of $chunk
It removes $/, which in this case is not just any newline, but also =15
> and I see when
> I comment this line out in testing, it causes the following =15 to be
> slurped into the chunk aswell, but I've not yet figured out why that
> newline character makes a difference since you've already told Perl
> you're splitting the file up by =15?
>
> > next unless $chunk =~ /\w/;
> > # process chunk
>
> So this says that we're going to process $chunk if it has an alphanumeric
> character in it, which it would if the first chunk has been successfuly
> slurped int?
The normal use of $/ implies the =15 would come after each of your records.
What you actually have is a =15 before each record. What's is the
difference? Well, you have an extra =15 at the start, which results (after
chomp) in an empty "record" the first time through the loop. So you have
to ignore that empty record. Of course, if it is legal to have empty
records in your file, then this method would ignore those as well, so you
can't use this method of ignoring the first (false) empty record if you
need to deal with real empty records.
Also, you don't have a =15 as the last line in your file, as you would
if =15 was the record ender rather than record starter. But that doesn't
cause any problems, because chomp only removed $/ if it is present and
does nothing if it is absent.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Fri, 5 Nov 2004 17:03:01 +0000 (UTC)
From: PerlFAQ Server <comdog@panix.com>
Subject: FAQ 8.25: How can I capture STDERR from an external command?
Message-Id: <cmgbo5$eld$1@reader1.panix.com>
This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.
--------------------------------------------------------------------
8.25: How can I capture STDERR from an external command?
There are three basic ways of running external commands:
system $cmd; # using system()
$output = `$cmd`; # using backticks (``)
open (PIPE, "cmd |"); # using open()
With system(), both STDOUT and STDERR will go the same place as the
script's STDOUT and STDERR, unless the system() command redirects them.
Backticks and open() read only the STDOUT of your command.
You can also use the open3() function from IPC::Open3. Benjamin Goldberg
provides some sample code:
To capture a program's STDOUT, but discard its STDERR:
use IPC::Open3;
use File::Spec;
use Symbol qw(gensym);
open(NULL, ">", File::Spec->devnull);
my $pid = open3(gensym, \*PH, ">&NULL", "cmd");
while( <PH> ) { }
waitpid($pid, 0);
To capture a program's STDERR, but discard its STDOUT:
use IPC::Open3;
use File::Spec;
use Symbol qw(gensym);
open(NULL, ">", File::Spec->devnull);
my $pid = open3(gensym, ">&NULL", \*PH, "cmd");
while( <PH> ) { }
waitpid($pid, 0);
To capture a program's STDERR, and let its STDOUT go to our own STDERR:
use IPC::Open3;
use Symbol qw(gensym);
my $pid = open3(gensym, ">&STDERR", \*PH, "cmd");
while( <PH> ) { }
waitpid($pid, 0);
To read both a command's STDOUT and its STDERR separately, you can
redirect them to temp files, let the command run, then read the temp
files:
use IPC::Open3;
use Symbol qw(gensym);
use IO::File;
local *CATCHOUT = IO::File->new_tempfile;
local *CATCHERR = IO::File->new_tempfile;
my $pid = open3(gensym, ">&CATCHOUT", ">&CATCHERR", "cmd");
waitpid($pid, 0);
seek $_, 0, 0 for \*CATCHOUT, \*CATCHERR;
while( <CATCHOUT> ) {}
while( <CATCHERR> ) {}
But there's no real need for *both* to be tempfiles... the following
should work just as well, without deadlocking:
use IPC::Open3;
use Symbol qw(gensym);
use IO::File;
local *CATCHERR = IO::File->new_tempfile;
my $pid = open3(gensym, \*CATCHOUT, ">&CATCHERR", "cmd");
while( <CATCHOUT> ) {}
waitpid($pid, 0);
seek CATCHERR, 0, 0;
while( <CATCHERR> ) {}
And it'll be faster, too, since we can begin processing the program's
stdout immediately, rather than waiting for the program to finish.
With any of these, you can change file descriptors before the call:
open(STDOUT, ">logfile");
system("ls");
or you can use Bourne shell file-descriptor redirection:
$output = `$cmd 2>some_file`;
open (PIPE, "cmd 2>some_file |");
You can also use file-descriptor redirection to make STDERR a duplicate
of STDOUT:
$output = `$cmd 2>&1`;
open (PIPE, "cmd 2>&1 |");
Note that you *cannot* simply open STDERR to be a dup of STDOUT in your
Perl program and avoid calling the shell to do the redirection. This
doesn't work:
open(STDERR, ">&STDOUT");
$alloutput = `cmd args`; # stderr still escapes
This fails because the open() makes STDERR go to where STDOUT was going
at the time of the open(). The backticks then make STDOUT go to a
string, but don't change STDERR (which still goes to the old STDOUT).
Note that you *must* use Bourne shell (sh(1)) redirection syntax in
backticks, not csh(1)! Details on why Perl's system() and backtick and
pipe opens all use the Bourne shell are in the versus/csh.whynot article
in the "Far More Than You Ever Wanted To Know" collection in
http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz . To capture a command's
STDERR and STDOUT together:
$output = `cmd 2>&1`; # either with backticks
$pid = open(PH, "cmd 2>&1 |"); # or with an open pipe
while (<PH>) { } # plus a read
To capture a command's STDOUT but discard its STDERR:
$output = `cmd 2>/dev/null`; # either with backticks
$pid = open(PH, "cmd 2>/dev/null |"); # or with an open pipe
while (<PH>) { } # plus a read
To capture a command's STDERR but discard its STDOUT:
$output = `cmd 2>&1 1>/dev/null`; # either with backticks
$pid = open(PH, "cmd 2>&1 1>/dev/null |"); # or with an open pipe
while (<PH>) { } # plus a read
To exchange a command's STDOUT and STDERR in order to capture the STDERR
but leave its STDOUT to come out our old STDERR:
$output = `cmd 3>&1 1>&2 2>&3 3>&-`; # either with backticks
$pid = open(PH, "cmd 3>&1 1>&2 2>&3 3>&-|");# or with an open pipe
while (<PH>) { } # plus a read
To read both a command's STDOUT and its STDERR separately, it's easiest
to redirect them separately to files, and then read from those files
when the program is done:
system("program args 1>program.stdout 2>program.stderr");
Ordering is important in all these examples. That's because the shell
processes file descriptor redirections in strictly left to right order.
system("prog args 1>tmpfile 2>&1");
system("prog args 2>&1 1>tmpfile");
The first command sends both standard out and standard error to the
temporary file. The second command sends only the old standard output
there, and the old standard error shows up on the old standard out.
--------------------------------------------------------------------
Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.
If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.
Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.
The perlfaq manual page contains the following copyright notice.
AUTHOR AND COPYRIGHT
Copyright (c) 1997-2002 Tom Christiansen and Nathan
Torkington, and other contributors as noted. All rights
reserved.
This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.
------------------------------
Date: Fri, 5 Nov 2004 10:04:02 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Finding consecutive lines
Message-Id: <slrncon93i.gvj.tadmc@magna.augustmail.com>
Peter <peter_holmes2003@yahoo.com> wrote:
> I want to find 5 consecutive lines with "abc" in first line, "def" in second,
> "efg" in third etc.
>
> How can I do something like this..
Buffer the 4 previous lines.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 5 Nov 2004 11:26:13 -0500
From: Brad Baxter <bmb@ginger.libs.uga.edu>
Subject: Re: Finding consecutive lines
Message-Id: <Pine.A41.4.58.0411051122180.41890@ginger.libs.uga.edu>
On Fri, 5 Nov 2004, Peter wrote:
> I need to parse a file looking for patterns in 5 consecutive lines.
> e.g.
>
> I want to find 5 consecutive lines with "abc" in first line, "def" in second,
> "efg" in third etc.
>
> How can I do something like this..
>
>
> Thanks in advance,
> Peter
>
What have you tried so far? :-)
Regards,
Brad
#!/usr/bin/perl
use warnings;
use strict;
my @f = qw( abc def ghi jkl mno );
my @s;
while( <DATA> ) {
push @s, $_;
shift @s if @s > 5;
next unless @s == 5;
print " @s\n" if
$s[0] =~ /$f[0]/ and
$s[1] =~ /$f[1]/ and
$s[2] =~ /$f[2]/ and
$s[3] =~ /$f[3]/ and
$s[4] =~ /$f[4]/;
}
__DATA__
hey abc
there def abc
june ghi def abc
bug jkl ghi def abc
lets mno jkl ghi def abc
get mno jkl ghi def
in mno jkl ghi
the mno jkl
mud mno
__END__
hey abc
there def abc
june ghi def abc
bug jkl ghi def abc
lets mno jkl ghi def abc
there def abc
june ghi def abc
bug jkl ghi def abc
lets mno jkl ghi def abc
get mno jkl ghi def
june ghi def abc
bug jkl ghi def abc
lets mno jkl ghi def abc
get mno jkl ghi def
in mno jkl ghi
bug jkl ghi def abc
lets mno jkl ghi def abc
get mno jkl ghi def
in mno jkl ghi
the mno jkl
lets mno jkl ghi def abc
get mno jkl ghi def
in mno jkl ghi
the mno jkl
mud mno
------------------------------
Date: Fri, 05 Nov 2004 17:20:46 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: Finding consecutive lines
Message-Id: <Xns95987D97C2E21dkwwashere@216.168.3.30>
Brad Baxter <bmb@ginger.libs.uga.edu> wrote:
> On Fri, 5 Nov 2004, Peter wrote:
>
>> I need to parse a file looking for patterns in 5 consecutive
>> lines. e.g.
>>
>> I want to find 5 consecutive lines with "abc" in first line,
>> "def" in second, "efg" in third etc.
>>
[snip]
>
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my @f = qw( abc def ghi jkl mno );
> my @s;
> while( <DATA> ) {
> push @s, $_;
> shift @s if @s > 5;
> next unless @s == 5;
> print " @s\n" if
> $s[0] =~ /$f[0]/ and
> $s[1] =~ /$f[1]/ and
> $s[2] =~ /$f[2]/ and
> $s[3] =~ /$f[3]/ and
> $s[4] =~ /$f[4]/;
>}
I like this a little better because it doesn't require maintenance if
the number of patterns changes.
#!/usr/bin/perl
use strict;
use warnings;
my @buffer;
my @pattern = qw(abc def ghi jkl mno);
while (<DATA>) {
push @buffer, $_;
next if @buffer != @pattern;
my $matches = grep /1/,
map $buffer[$_] =~ /$pattern[$_]/,
0 .. $#pattern;
print @buffer if $matches == @pattern;
shift @buffer;
}
__DATA__
jdfk jkdfh bl
dfjkv dfjk
dfjdj
abc these
def are
ghi the
jkl lines
mno we want
hj dfvhj d
jkfh vblsdjk
jdkf hld
------------------------------
Date: 05 Nov 2004 17:57:09 GMT
From: ctcgag@hotmail.com
Subject: Re: Finding consecutive lines
Message-Id: <20041105125709.475$Si@newsreader.com>
peter_holmes2003@yahoo.com (Peter) wrote:
> I need to parse a file looking for patterns in 5 consecutive lines.
> e.g.
>
> I want to find 5 consecutive lines with "abc" in first line, "def" in
> second, "efg" in third etc.
>
> How can I do something like this..
If your file fits in memory:
warn "Untested";
$file =~ /^(.*abc.*\n.*def.*\n.*efg.*)$/m;
If course you presumably want to do something with these lines, but you
didn't say what that was.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
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 7364
***************************************