If...else and exit;

M

MattJ83

Hi,

I've written some code which is almost complete but am having trouble
at the final hurdle.

My code looks for the string FASTSEARCH - if this line is not here - i
want the program to exit.
If there is an error message where the FASTSEARCH string should be -
return this value and carry on with the script. If there is no error OR
FASTSEARCH - the script should terminate.
The script seems to return the correct values when i ask it to print
them - but i am trying to put them into a database - and it isn't
working as the 'print on screen' is.

Any help would be appreciated.

Working code:

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

my @filenames = </home/username/logs/*.log>;
foreach my $filename (@filenames)
{
open my $LOG, '<', $filename or die "can't open $filename: $!\n";
print "$filename\n";

my ($fast, $error, $inversions);
while(<$LOG>) {

if (/updates table/) {
/Rows in fast-search index updates table: (.*)/;
print "$1\n";

}
if (/deletions table/) {
/Rows in fast-search index deletions table: (.*)/;
print "$1\n";

}
if (/elapsed/) {
if ($fast or $error) {
/deltas: elapsed=(.*), cpu=(.*), user=(.*), sys=(.*), n=(.*)/;
print "$1\n";

last;
}
}
if (/FASTSEARCH/) {
$fast = $_;
}
if (/conflicting/) {
$error = $_;
print "Conflicting Lock Error Found\n";
}
}
while (<$LOG>) {
if (/inversions/) {
$inversions = $_;
}
}

if (!$fast) { <----------if /FASTSEARCH/ is not
found i want the program to exit.
exit; } elsif ($error) {next;} <----------if /conflicting/ is
found I want the script to keep running
if (!$inversions) {
exit; }

close($LOG);

}

Test data:

_________$LOG__________

string string
Rows in fast-search index updates table: 8
Rows in fast-search index deletions table: 6
string
string

string
deltas: elapsed=(0), cpu=(0), user=(0), sys=(0), n=(0)
string
FASTSEARCH <-------this may not be here - if not
exit....else if error
message is here
(/conflicting/) return error
message and go to next log
(carry on)
deltas: elapsed=(0), cpu=(0), user=(0), sys=(0), n=(0)
inversions

deltas: elapsed=(0), cpu=(0), user=(0), sys=(0), n=(0)
 
D

Dave

MattJ83 said:
Hi,

I've written some code which is almost complete but am having trouble
at the final hurdle.

My code looks for the string FASTSEARCH - if this line is not here - i
want the program to exit.
If there is an error message where the FASTSEARCH string should be -
return this value and carry on with the script. If there is no error OR
FASTSEARCH - the script should terminate.
The script seems to return the correct values when i ask it to print
them - but i am trying to put them into a database - and it isn't
working as the 'print on screen' is.

Any help would be appreciated.

Working code:

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

my @filenames = </home/username/logs/*.log>;
foreach my $filename (@filenames)
{
open my $LOG, '<', $filename or die "can't open $filename: $!\n";
print "$filename\n";

my ($fast, $error, $inversions);
while(<$LOG>) {

if (/updates table/) {
/Rows in fast-search index updates table: (.*)/;
print "$1\n";

}
if (/deletions table/) {
/Rows in fast-search index deletions table: (.*)/;
print "$1\n";

}
if (/elapsed/) {
if ($fast or $error) {
/deltas: elapsed=(.*), cpu=(.*), user=(.*), sys=(.*), n=(.*)/;
print "$1\n";

last;
}
}
if (/FASTSEARCH/) {
$fast = $_;
}
if (/conflicting/) {
$error = $_;
print "Conflicting Lock Error Found\n";
}
}
while (<$LOG>) {
if (/inversions/) {
$inversions = $_;
}
}

if (!$fast) { <----------if /FASTSEARCH/ is not
found i want the program to exit.
exit; } elsif ($error) {next;} <----------if /conflicting/ is
found I want the script to keep running
if (!$inversions) {
exit; }

close($LOG);

}

Test data:

_________$LOG__________

string string
Rows in fast-search index updates table: 8
Rows in fast-search index deletions table: 6
string
string

string
deltas: elapsed=(0), cpu=(0), user=(0), sys=(0), n=(0)
string
FASTSEARCH <-------this may not be here - if not
exit....else if error
message is here
(/conflicting/) return error
message and go to next log
(carry on)
deltas: elapsed=(0), cpu=(0), user=(0), sys=(0), n=(0)
inversions

deltas: elapsed=(0), cpu=(0), user=(0), sys=(0), n=(0)

The problem is in muddled thinking rather than the code. Reread your own
description.
 
B

Brian McCauley

MattJ83 said:
I've written some code which is almost complete...

...ly unreadable!

I've moved the whitespace about so that it's readble.

I've also changed the comment marker to # rather than <----------

I don't know what version you have of perl that treats <---------- as a
comment but I've never seen one.

Note: I've not tried to understand or fix your code (yet) I've just
cleaned up the formatting so anyone who was inclined to help you would
not have to do so.

Note that by reapeatedly posting code that so badly indented you are
rappidly exhasting your God-given (or is that Camel-given) quota of
good will.

use strict;
use warnings;

my @filenames = <*.log>;
foreach my $filename (@filenames) {

open my $LOG, '<', $filename or die "can't open $filename: $!\n";
print "$filename\n";

my ($fast, $error, $inversions);
while(<$LOG>) {

if (/updates table/) {
/Rows in fast-search index updates table: (.*)/;
print "$1\n";
}

if (/deletions table/) {
/Rows in fast-search index deletions table: (.*)/;
print "$1\n";
}

if (/elapsed/) {
if ($fast or $error) {
/deltas: elapsed=(.*), cpu=(.*), user=(.*), sys=(.*), n=(.*)/;
print "$1\n";

last;
}
}

if (/FASTSEARCH/) {
$fast = $_;
}

if (/conflicting/) {
$error = $_;
print "Conflicting Lock Error Found\n";
}
}

while (<$LOG>) {
if (/inversions/) {
$inversions = $_;
}
}

if (!$fast) {
# if /FASTSEARCH/ is no found i want the program to exit.
exit;
} elsif ($error) {
# if /conflicting/ is found I want the script to keep running
next;
}
if (!$inversions) {
exit;
}

close($LOG);

}
__END__
 
B

Brian McCauley

MattJ83 said:
The script seems to return the correct values when i ask it to print
them - but i am trying to put them into a database - and it isn't
working as the 'print on screen' is.

I suspect this is untrue.
Any help would be appreciated.

I sugesst you produce a _minimal_ but _complete_ (cleanly indented)
example that you think should write to a database and confirm that is
fails as you expect.

Then comment out the database operations and replace them with print
statements. Do not make any other changes.

Confirm that this appears not to fail in the same way.

Then post the script and the data here _unaltered_. If you want
comments in your exemplar script then put comments in the script, do
_not_ insert syntatically invalid prose into the post.
 
B

Brian McCauley

Brian said:
Then comment out the database operations and replace them with print
statements. Do not make any other changes.

By which I mean that the print() statements should print out exactly
what was to be written to the database in exactly the same place in the
code as it would have been written.
 
B

Brian McCauley

Brian said:
if (/updates table/) {
/Rows in fast-search index updates table: (.*)/;
print "$1\n";
}

You should _never_ use $1 etc unless you know the match succeded.

You probaly wanted to say:

if (/Rows in fast-search index updates table: (.*)/) {
print "$1\n";
}
 
M

MattJ83

By which I mean that the print() statements should print out exactly
what was to be written to the database in exactly the same place in the
code as it would have been written.

As seen below:

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

my @filenames = </home/username/logs/*.log>;
foreach my $filename (@filenames) {


open my $LOG, '<', $filename or die "can't open $filename: $!\n";

my ($fast, $error, $update, $deletion, $elapsed);
while(<$LOG>) {
if (/Rows in fast-search index updates table: (.*)/) {
$update = $1;
}

if (/Rows in fast-search index deletions table: (.*)/) {
$deletion = $1;
}

if (/elapsed/) {
if ($fast or $error) {
/deltas: elapsed=(.*), cpu=(.*), user=(.*), sys=(.*),
n=(.*)/;
$elapsed = $1;

last;
}
}

if (/FASTSEARCH/) {
$fast = $_;
}

if (/conflicting/) {
$error = $_;
}
}
close($LOG);

print ("$filename\n", "$update\n", "$deletion\n", "$elapsed\n",
"$error\n");

}

__________END_________


_____one.log_____
Rows in fast-search index updates table: 50
Rows in fast-search index deletions table: 5
deltas: elapsed=0, cpu=0, user=0, sys=0, n=0
FASTSEARCH
deltas: elapsed=1, cpu=1, user=1, sys=1, n=1
inversions
deltas: elapsed=2, cpu=2, user=2, sys=2, n=2

______two.log________
Rows in fast-search index updates table: 10
Rows in fast-search index deletions table: 2
deltas: elapsed=0, cpu=0, user=0, sys=0, n=0
conflicting
deltas: elapsed=1, cpu=1, user=1, sys=1, n=1
inversions
deltas: elapsed=2, cpu=2, user=2, sys=2, n=2


Ok, so the code above will get the second of the elapsed (after
FASTSEARCH or conflicting).
I would like the code to exit if it can't find FASTSEARCH or
conflicting. I would also like it to exit if it can't find 'inversions'
(i can't seem to pick this up with the code yet though -im struggling
to see where i would tell the code to look for inversions).


ps - thank you for shouwing me the correct indentation - people had
been saying it was quite bad - but weren't explaining what the format
should be like!
 
K

Klaus

MattJ83 said:
ps - thank you for shouwing me the correct indentation - people had
been saying it was quite bad - but weren't explaining what the format
should be like!

perldoc perlstyle
 
J

Joe Smith

MattJ83 said:
I would like the code to exit if it can't find FASTSEARCH or
conflicting. I would also like it to exit if it can't find 'inversions'

What do you mean?

Do you want to abort and not read two.log if "FASTSEARCH"
or "conflicting" is not found in the first file? Just check
at the bottom of the loop that reads each file.

If you want to exit if it can't find "FASTSEARCH" or "conflicting"
in any of the files, then what you've got already does that.
It reads through each file until EOF, and stops when there
are no more files.

You'll need to rephrase your request for us to understand it.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top