Empty Variable ?

M

Miles

Looking for some help please.

Im using "Shell" to tail a logfile into a $Variable - but my logfiles
rotate, so an empty file might get called in... which I need to check for.

use Shell;
$lastlines = tail(-n $linestoread, $filename);
if ($lastlines == "") {
"Print - Says, the current logfile appears to be EMPTY (logs are
rotated at midnight!)\r\n|");
} else {
"Print - Says, these are the last $linestoread lines I have
logged: \r\n" .
"$lastlines|");
}

If log is empty, then $lastlines returns zero and the first command works
ok.
But if log is full, then $lastlines > zero, and I get errors.
_quote_
" isn't numeric in numeric eq (==) at
/home/testing/scripts/logger.pl line 87.
_unquote_

This is probably an easy one, but any assistance is appreciated.

Cheers,

Miles
n00b !
 
M

Miles

Hmm...

Sorry, figured it all out... was really basic !

if ($lastlines == "") should have been if ($lastlines =~ "")

Yes, was the noob factor.

Cheers,

Miles...
 
M

Mark Jason Dominus

Looking for some help please.
if ($lastlines == "") {

'==' is only for comparing numbers. To compare two strings, you must
use 'eq':

if ($lastlines eq "") {
use Shell;
$lastlines = tail(-n $linestoread, $filename);

It seems to me that you want
$lastlines = tail("-n", $linestoread, $filename);

here. If '-n $linestoread' is working, it's only because you were
fabulously lucky.
 
D

Dave Weaver

Hmm...

Sorry, figured it all out... was really basic !

if ($lastlines == "") should have been if ($lastlines =~ "")

I doubt that you want that either. Try it:

$x = "not empty";
print "oops!" if $x =~ "";

You problem is that you're using the '==' operator on a string.
'==' and its companions like '>' and '<' are for numeric comparisons.
Use the 'eq' operator (and 'gt', 'lt' etc) to compare strings:

$x = "not blank";
print "empty" if $x eq "";

Dave.
 
E

Erik Tank

I've always preferred this method for finding empty lines:
print("Empty line\n") if $line =~ /^$/;
 
T

Tad McClellan

Erik Tank said:
I've always preferred this method for finding empty lines:
print("Empty line\n") if $line =~ /^$/;


That will report the string "\n" as being empty.


I use:

print("Empty line\n") unless length $line;

Which won't.
 
M

Miles

Thanks for the replies,

I actually ended up using if ($lastlines !~ /./)
Now the else statement works too.

Cheers,

Miles.
 
M

Miles

Mark,

I have been 'fabulously lucky' with perl so far ;)

Thanks for your reply, and I did adjust to:
$lastlines = tail("-n", $linestoread, $filename);

Cheers,

Miles.
noob.
 
U

Uri Guttman

stop top posting. read the group guidelines (posted regularly)

M> Thanks for the replies,
M> I actually ended up using if ($lastlines !~ /./)
M> Now the else statement works too.

ever heard of unless?

and how many times must you be told that eq is simpler and faster to
check for an empty string? i avoid !~ like the plague as it doesn't read
well in english. even a simple ! $foo =~ /bar/ reads better but unless
reads the best.

<snip of massive quote>

uri
 
M

Miles

Scott,
Is there a reason why you aren't using
if (! $lastlines)
if (length($lastlines) == 0)

yeah, im new to programming and didn't know all these options ppl have
posted.

my prob was I didn't understand difference between "==" and "eq".

thanks for the pointer, I will definately be trying them all.

thanks,

Miles...
 
U

Uri Guttman

M> yeah, im new to programming and didn't know all these options ppl have
M> posted.

M> my prob was I didn't understand difference between "==" and "eq".

M> thanks for the pointer, I will definately be trying them all.

trying them all is not how to learn progamming. each comparison op is
there for a very good reason. learn why they are there and use the
proper one in the proper place. coding is not a tasting party where you
try everything, you have to be the chef.

uri
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top