Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Pattern Matching and skipping

Reply
Thread Tools

Pattern Matching and skipping

 
 
Tad McClellan
Guest
Posts: n/a
 
      09-07-2006
MattJ83 <> wrote:

> Right - i thought it might be btter if i just paste all of the code i
> have done:



You should write your code so that its structure can be seen.

Each new block should increase the indent level.


> while (<LOG>) {
> if (/updates table/) {
> my @lines = split /\n/; {
> foreach my $info (@lines) {
>
> while (<LOG>) {
> if (/elapsed/) {
> my @lines = split /\n/; {
> foreach my $elapsed1 (@lines) {



while (<LOG>) {
if (/updates table/) {
my @lines = split /\n/; { <== what is that "{" there for?
foreach my $info (@lines) {

while (<LOG>) {
if (/elapsed/) {
my @lines = split /\n/; {
foreach my $elapsed1 (@lines) {

Now it is easy to see that control can never get to /elapsed/
unless /updates table/ matches first.

Read the line once, test it many times.

while (<LOG>) {
if (/updates table/) {
# do stuff
}
elsif (/elapsed/) {
# do other stuff
}


> I just can't get the else or elseif statements to work...........



That is because you are not writing them correctly.


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
MattJ83
Guest
Posts: n/a
 
      09-07-2006

>
> Sure, you threw out the baby with the bath water. The assignment to
> @lines also goes away if you just delete the line.
>
> The point is, @lines will only ever contain one element, the current
> line minus its line feed. The same would be achieved by (untested)
>
> while ( <LOG> ) {
> chomp;
> if ( /elapsed/ ) {
> my @lines = ( $_);
>
> I seem to remember that a loop over @lines follows. Both the variable
> @lines and the loop can presumably go away too.
>
> Anno


By this do you mean i can replace that looping repetitious code by
using chomp; and ($_); ? Is ($_) just a short hand way of writing the
full variable or do u actually mean do this and then declare the $
variables somewhere else?

 
Reply With Quote
 
 
 
 
MattJ83
Guest
Posts: n/a
 
      09-07-2006

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

my @files= </home/mmj19903/logs/*.log>;
foreach my $file (@files) {

open (LOG,</home/mmj19903/logs/*.log>) or die "can't open LOG: $!\n";

while (<LOG>) {
chomp;
if (/updates table/){
my @lines = ( $_); ;
foreach my $info (@lines) {

while (<LOG>) {
chomp;
if (/elapsed/) {
my @lines = ($_);
foreach my $elapsed1 (@lines) {

............
I presume this is what you mean't by the chomp - or did u mean
something more simple still?
So without the foreach line - i just get a global symbol $info (etc)
requires explicit package name....

 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      09-07-2006
MattJ83 wrote:
> #!/usr/central/bin/perl
> use strict ;
> #use warnings;
> use DBI;
>
> my @files= </home/mmj19903/logs/*.log>;
> foreach my $file (@files) {
>
> open (LOG,</home/mmj19903/logs/*.log>) or die "can't open LOG: $!\n";


Read my earlier response about this line. You are not opening $file.


DS
 
Reply With Quote
 
MattJ83
Guest
Posts: n/a
 
      09-07-2006

David Squire wrote:
> MattJ83 wrote:
> > #!/usr/central/bin/perl
> > use strict ;
> > #use warnings;
> > use DBI;
> >
> > my @files= </home/mmj19903/logs/*.log>;
> > foreach my $file (@files) {
> >
> > open (LOG,</home/mmj19903/logs/*.log>) or die "can't open LOG: $!\n";

>
> Read my earlier response about this line. You are not opening $file.
>
>
> DS


I wasn't sure what you were gettign at with this line David - it works
for me - this code so far does what i want it to.....it opens the log
file puts the path of the log into a database field, and pulls out the
lines that have the soecific words in them and places these lines ina
database CLOB field. This is what i want - it repeats for each log in
the directory.....

Im a bit reluctant to change the code when this one works ok for me...
?

 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      09-07-2006
MattJ83 wrote:
> David Squire wrote:
>> MattJ83 wrote:
>>> #!/usr/central/bin/perl
>>> use strict ;
>>> #use warnings;
>>> use DBI;
>>>
>>> my @files= </home/mmj19903/logs/*.log>;
>>> foreach my $file (@files) {
>>>
>>> open (LOG,</home/mmj19903/logs/*.log>) or die "can't open LOG: $!\n";

>> Read my earlier response about this line. You are not opening $file.
>>
>>
>> DS

>
> I wasn't sure what you were gettign at with this line David - it works
> for me - this code so far does what i want it to.....it opens the log
> file puts the path of the log into a database field, and pulls out the
> lines that have the soecific words in them and places these lines ina
> database CLOB field. This is what i want - it repeats for each log in
> the directory.....
>
> Im a bit reluctant to change the code when this one works ok for me...


It will work so long as there is only one file that matches the pattern
in the glob </home/mmj19903/logs/*.log>. The lines above suggest that
you want to open each of the files in @files in turn. Your code does not
do that. You don't use the variable $file from the loop 'foreach my
$file (@files) {...}' anywhere. What is that loop for then?

DS
 
Reply With Quote
 
MattJ83
Guest
Posts: n/a
 
      09-07-2006

David Squire wrote:
> MattJ83 wrote:
> > David Squire wrote:
> >> MattJ83 wrote:
> >>> #!/usr/central/bin/perl
> >>> use strict ;
> >>> #use warnings;
> >>> use DBI;
> >>>
> >>> my @files= </home/mmj19903/logs/*.log>;
> >>> foreach my $file (@files) {
> >>>
> >>> open (LOG,</home/mmj19903/logs/*.log>) or die "can't open LOG: $!\n";
> >> Read my earlier response about this line. You are not opening $file.
> >>
> >>
> >> DS

> >
> > I wasn't sure what you were gettign at with this line David - it works
> > for me - this code so far does what i want it to.....it opens the log
> > file puts the path of the log into a database field, and pulls out the
> > lines that have the soecific words in them and places these lines ina
> > database CLOB field. This is what i want - it repeats for each log in
> > the directory.....
> >
> > Im a bit reluctant to change the code when this one works ok for me...

>
> It will work so long as there is only one file that matches the pattern
> in the glob </home/mmj19903/logs/*.log>. The lines above suggest that
> you want to open each of the files in @files in turn. Your code does not
> do that. You don't use the variable $file from the loop 'foreach my
> $file (@files) {...}' anywhere. What is that loop for then?
>
> DS


As far as my limited PERL knowledge takes me (not far) i was under the
impression that that first few lines declares the directory then tells
it to loop the path name of the log file in the directory to the LOGID
column then open the log file and pull those lines (with /elapsed/ etc)
into the corresponding fields. This then repeats for the next log file
and then next.......it is only when the /pattern/ isn't matched that
the log file is not being recorded into the directory......

Thanks for your info...!!

 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      09-07-2006
MattJ83 wrote:

> As far as my limited PERL knowledge takes me (not far) i was under the
> impression that that first few lines declares the directory then tells
> it to loop the path name of the log file in the directory to the LOGID
> column then open the log file and pull those lines (with /elapsed/ etc)
> into the corresponding fields. ...


[snip]

Here is an annotated version of those first few lines:

> my @files= </home/USERNAME/logs/*.log>;


Create an array called @files and initialise it with a list of all the
files or directories that match the pattern "/home/USERNAME/logs/*.log",
i.e. the same things you would see if you typed "ls
/home/USERNAME/logs/*.log" in your shell. This is called a glob.

> foreach my $file (@files) {


For each element of the array @files, create an alias for that element
called $file, and execute the code in the block that follows. You seem
not ever to use this - though if I were you I would.

> open (LOG,</home/USERNAME/logs/*.log>) or die "can't open LOG: $!\n";


Open a file handle LOG whose filename is given by the expression
"</home/USERNAME/logs/*.log>". This glob, identical to the one above, is
used here in scalar context, so what open gets is the next element of
the list in the glob... which means that you code actually does what you
want - to my surprise (I did not know about this behaviour of glob in a
scalar context)!

I still think it would be clearer to write:

my @filenames= </home/USERNAME/logs/*.log>;
foreach my $filename (@filenames) {
open my $LOG, '<', $filename or die "can't open $filename: $!\n";
.... # using $LOG wherever you had LOG
}

If you don't do that, your first two lines are redundant - there is
certainly no need to do the glob twice.


DS
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      09-07-2006
MattJ83 <> wrote in comp.lang.perl.misc:
>
> >
> > Sure, you threw out the baby with the bath water. The assignment to
> > @lines also goes away if you just delete the line.
> >
> > The point is, @lines will only ever contain one element, the current
> > line minus its line feed. The same would be achieved by (untested)
> >
> > while ( <LOG> ) {
> > chomp;
> > if ( /elapsed/ ) {
> > my @lines = ( $_);
> >
> > I seem to remember that a loop over @lines follows. Both the variable
> > @lines and the loop can presumably go away too.
> >
> > Anno

>
> By this do you mean i can replace that looping repetitious code by
> using chomp; and ($_); ? Is ($_) just a short hand way of writing the
> full variable or do u actually mean do this and then declare the $
> variables somewhere else?


Please use full pronouns.

No, I don't mean you should literally replace your code with mine.
All I'm saying is that my code does the same thing yours does but
without the spurious split.

You should reduce the code much more. I think another branch of
this thread is quite on the mark.

Anno
 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      09-07-2006
David Squire wrote:

> If you don't do that, your first two lines are redundant - there is
> certainly no need to do the glob twice.


Ah. Not quite, since the loop means you call the second glob the right
number of times. Still, I think it is hard to understand - at least I
have shown that it was for me


DS
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex testing and UTF8 awarenes or Regex and numeric pattern matching sln@netherlands.com Perl Misc 2 03-10-2009 03:51 AM
Help with Pattern matching. Matching multiple lines from while reading from a file. Bobby Chamness Perl Misc 2 05-03-2007 06:02 PM
Pattern Matching Given # of Characters and no String Input; use RegularExpressions? Synonymous Python 10 04-22-2005 07:56 AM
[perl-python] text pattern matching, and expressiveness Xah Lee Python 4 02-11-2005 09:11 PM
Pattern matching : not matching problem Marc Bissonnette Perl Misc 9 01-13-2004 05:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57