Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Empty Variable ?

Reply
Thread Tools

Empty Variable ?

 
 
Miles
Guest
Posts: n/a
 
      01-06-2004
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 !


 
Reply With Quote
 
 
 
 
Miles
Guest
Posts: n/a
 
      01-06-2004
Hmm...

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

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

Yes, was the noob factor.

Cheers,

Miles...


"Miles" <(E-Mail Removed)> wrote in message
news:3ffaba36$0$22625$(E-Mail Removed) om.au...
> 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 !
>
>



 
Reply With Quote
 
 
 
 
Mark Jason Dominus
Guest
Posts: n/a
 
      01-06-2004

In article <3ffaba36$0$22625$(E-Mail Removed) u>,
Miles <(E-Mail Removed)> wrote:
>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.



 
Reply With Quote
 
Dave Weaver
Guest
Posts: n/a
 
      01-06-2004
On Wed, 7 Jan 2004 00:08:25 +1000, Miles <(E-Mail Removed)> wrote:
> 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.



 
Reply With Quote
 
Erik Tank
Guest
Posts: n/a
 
      01-06-2004
I've always preferred this method for finding empty lines:
print("Empty line\n") if $line =~ /^$/;

On Wed, 7 Jan 2004 00:08:25 +1000, "Miles"
<(E-Mail Removed)> wrote:

>Hmm...
>
>Sorry, figured it all out... was really basic !
>
>if ($lastlines == "") should have been if ($lastlines =~ "")
>
>Yes, was the noob factor.
>
>Cheers,
>
>Miles...
>
>
>"Miles" <(E-Mail Removed)> wrote in message
>news:3ffaba36$0$22625$(E-Mail Removed). com.au...
>> 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 !
>>
>>

>


 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-06-2004
Erik Tank <(E-Mail Removed)> wrote:

> 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.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Miles
Guest
Posts: n/a
 
      01-06-2004
Thanks for the replies,

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

Cheers,

Miles.


"Miles" <(E-Mail Removed)> wrote in message
news:3ffac06e$0$22620$(E-Mail Removed) om.au...
> Hmm...
>
> Sorry, figured it all out... was really basic !
>
> if ($lastlines == "") should have been if ($lastlines =~ "")
>
> Yes, was the noob factor.
>
> Cheers,
>
> Miles...
>
>
> "Miles" <(E-Mail Removed)> wrote in message
> news:3ffaba36$0$22625$(E-Mail Removed) om.au...
> > 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 !
> >
> >

>
>



 
Reply With Quote
 
Miles
Guest
Posts: n/a
 
      01-06-2004
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.

"Mark Jason Dominus" <(E-Mail Removed)> wrote in message
news:btekak$d4u$(E-Mail Removed)...
>
> In article <3ffaba36$0$22625$(E-Mail Removed) u>,
> Miles <(E-Mail Removed)> wrote:
> >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.
>
>
>



 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      01-07-2004
>>>>> "M" == Miles <(E-Mail Removed)> writes:

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

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Miles
Guest
Posts: n/a
 
      01-07-2004
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...


 
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
DetailsView ASP Control and an empty field not an empty record. ButlerDJIAM ASP .Net 0 11-09-2006 06:40 PM
Altova Mapforce - xml 2 xml map: empty elements output although input element is not empty Lukas XML 3 11-10-2005 02:25 PM
empty lists vs empty generators Brian Roberts Python 12 05-04-2005 08:59 PM
Check if a directory is empty and empty it Marcia Hon C Programming 8 02-14-2004 03:53 AM
empty/non-empty element John XML 1 07-16-2003 10:23 AM



Advertisments