Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Pattern Matching and skipping

 
 
MattJ83
Guest
Posts: n/a
 
      09-07-2006
Ok guys,
Im not brushing your info to one side...but my plan was to get the code
running and then try and smarten it up....At the moment im just trying
to write something very basic that does the job.....which i almost
have.
I have a time constraint, so wanted to write some code to get the
information into the database and then i can start playing with the
code...

If theres just no way of oing what I would like with this piece of code
inparticular then i obviously have to go back to the drawing board and
look at it again......i just thought there should be some kind of way
to 'skip' over a /pattern/ to allow the script to keep running...

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-07-2006
MattJ83 <(E-Mail Removed)> wrote:

> i just thought there should be some kind of way
> to 'skip' over a /pattern/ to allow the script to keep running...



There is a way to do that, and it has already been shown to you.

If it is not working, post another short and complete program that
we can run that "doesn't work", and we will help you fix it.

Have you seent the Posting Guidelines that are posted here frequently?


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
David Squire
Guest
Posts: n/a
 
      09-07-2006
MattJ83 wrote:
> Ok guys,
> Im not brushing your info to one side...but my plan was to get the code
> running and then try and smarten it up....At the moment im just trying
> to write something very basic that does the job.....which i almost
> have.
> I have a time constraint, so wanted to write some code to get the
> information into the database and then i can start playing with the
> code...
>
> If theres just no way of oing what I would like with this piece of code
> inparticular then i obviously have to go back to the drawing board and
> look at it again......i just thought there should be some kind of way
> to 'skip' over a /pattern/ to allow the script to keep running...
>


Sure there is:

if (/pattern/) {
# do whatever you want when there is a match
}
else {
# do what is appropriate when there isn't a match, which could be as
simple as "next;" to go on to the next loop iteration.
}

If you don't want to do anything when there is no match, you could just have

next if !/pattern/;

as the first line of your loop.

You need to be willing to actually understand what you are doing though.
This is a group for helping people with the Perl programming. Various
people here have been trying to nudge you towards a solution to your
problem, and towards better Perl programming practice in general.

I learn something every day from participating here.

On the other hand, perhaps you are looking for http://jobs.perl.org/


DS
 
Reply With Quote
 
MattJ83
Guest
Posts: n/a
 
      09-07-2006
> There is a way to do that, and it has already been shown to you.

Tad, is this the If, elsif statements ? I gave it a try but it didn't
seem to like it - I don't think i was putting in the right stuff...

while (<LOG>) {
if (/updates table/) {
# do stuff <--- i was putting similar code to what i had
already done
}
elsif (/elapsed/) {
# do other stuff
}

Eg -
while (<LOG>) {
if (/updates table/) { #if updates table
in file
chomp; #take it out
my @lines = ( $info); ; #place it in my
@lines and in variable $info
foreach my $info (@lines) { #loop my $info in each
file
}
elsif (/elapsed/) {
chomp;
my @lines = ($_);
foreach my $elapsed1 (@lines) {
}


I thought i still needed 'my $info' and my $elapsed1 as i have the
INSERT statement call these variables to insert them into the database.
and the foreach loops it so that it looks at all of the logs doesn't
it?

This is how i read it.....but i know im probably wrong - i have studied
programming and know what its supposed to do in theory but in practice
im a bit rubbish!

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

>
> Eg -
> while (<LOG>) {
> if (/updates table/) { #if updates table
> in file


Not in the file, in the *line* from log currently being processed (which
is implicitly stored in $_)

> chomp; #take it out


This doesn't take anything out, it just "chomps" $_, the current line,
i.e. it removes any end of line character

> my @lines = ( $info); ; #place it in my
> @lines and in variable $info


One: where does $info get declared and get a value? Two: why are you
placing this single scalar value in an array @lines? @lines is now an
array with a single element, $lines[0], which has the same value as $info.

> foreach my $info (@lines) { #loop my $info in each


Why are you looping over an array that has one and only one element?

> file
> }


Why are you ending the loop here? This (unnecessary) loop has no content.

> elsif (/elapsed/) {
> chomp;
> my @lines = ($_);
> foreach my $elapsed1 (@lines) {
> }
>
>
> I thought i still needed 'my $info' and my $elapsed1 as i have the
> INSERT statement call these variables to insert them into the database.
> and the foreach loops it so that it looks at all of the logs doesn't
> it?
>
> This is how i read it.....but i know im probably wrong - i have studied
> programming and know what its supposed to do in theory but in practice
> im a bit rubbish!
>

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-07-2006
MattJ83 <(E-Mail Removed)> wrote:
>> There is a way to do that, and it has already been shown to you.

>
> Tad, is this the If, elsif statements ?



Yes.


> I gave it a try but it didn't
> seem to like it



Then you didn't do it right.

I thought I asked you for a short and complete program that we
can run that demonstrates that it "didn't like it"?


Have you seen the Posting Guidelines that are posted here frequently?

(that is NOT a rhetorical question.)


> while (<LOG>) {
> if (/updates table/) {
> # do stuff <--- i was putting similar code to what i had
> already done
> }
> elsif (/elapsed/) {
> # do other stuff
> }



Here is a short and complete program that *you* can run that
illustrates that it "likes it" just fine...


------------------------
#!/usr/bin/perl
use warnings;
use strict;

while (<DATA>) {
if (/updates table/) {
print "MATCHED: $_";
}
elsif (/elapsed/) {
print "MATCHED: $_";
}
else {
print "NO MATCH: $_";
}
}


__DATA__
nothing interesting here
this has updates table in it
nothing here either
now we have elapsed
now we don't
------------------------


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

David Squire wrote:
> MattJ83 wrote:
> > Ok guys,
> > Im not brushing your info to one side...but my plan was to get the code
> > running and then try and smarten it up....At the moment im just trying
> > to write something very basic that does the job.....which i almost
> > have.
> > I have a time constraint, so wanted to write some code to get the
> > information into the database and then i can start playing with the
> > code...
> >
> > If theres just no way of oing what I would like with this piece of code
> > inparticular then i obviously have to go back to the drawing board and
> > look at it again......i just thought there should be some kind of way
> > to 'skip' over a /pattern/ to allow the script to keep running...
> >

>
> Sure there is:
>
> if (/pattern/) {
> # do whatever you want when there is a match
> }
> else {
> # do what is appropriate when there isn't a match, which could be as
> simple as "next;" to go on to the next loop iteration.
> }
>
> If you don't want to do anything when there is no match, you could just have
>
> next if !/pattern/;
>
> as the first line of your loop.
>
> You need to be willing to actually understand what you are doing though.
> This is a group for helping people with the Perl programming. Various
> people here have been trying to nudge you towards a solution to your
> problem, and towards better Perl programming practice in general.
>
> I learn something every day from participating here.
>
> On the other hand, perhaps you are looking for http://jobs.perl.org/
>
>
> DS

What you have mentioned here is what i've been trying to do all day but
with out any success - im actually a bit happier now because i can see
i am on the right lines - I was trying to do this yesterday but i think
because my knowledge is still quite limited I didn't have enough belief
that i was doing the right thing.........

I admit that i was getting completely confused with all the loops in
that code! I'll have another look tomorrow at these items and see if i
can put them in the right place with the correct syntax.....

I do appreciate your help in guiding me through this code - so far i've
just had a book and been told to right a script!

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-07-2006
MattJ83 <(E-Mail Removed)> wrote:

> I do appreciate your help in guiding me through this code - so far i've
> just had a book and been told to right a script!



Writing a script is not too difficult.

Righting a script is more difficult.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
MattJ83
Guest
Posts: n/a
 
      09-08-2006

Tad McClellan schrieb:

> MattJ83 <(E-Mail Removed)> wrote:
>
> > I do appreciate your help in guiding me through this code - so far i've
> > just had a book and been told to right a script!

>
>
> Writing a script is not too difficult.
>
> Righting a script is more difficult.
>

sorry - yes, write.... !

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

> while (<DATA>) {
> if (/updates table/) {
> print "MATCHED: $_";
> }
> elsif (/elapsed/) {
> print "MATCHED: $_";
> }
> else {
> print "NO MATCH: $_";
> }
> }


I understand this but, when i am trying to do something similar for my
specific code im struggling.

while (<$LOG>) {
if (/updates table/) {
my @lines = $_;
foreach my $file (@lines) {
# }else { next if !(/updates table/);
# }

With the if, else statements they are blocking my declared variables
($file) so that the sql statement can't see them. Im getting an
explicit package error.

Surely, i have to find /updates table/ - put the results in array
@lines - allow $file to read this array and then place this $file in my
SQL statement. The curley brackets prevent this?

 
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