Empty Variable ?

Discussion in 'Perl Misc' started by Miles, Jan 6, 2004.

  1. Miles

    Miles Guest

    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 !
    Miles, Jan 6, 2004
    #1
    1. Advertising

  2. Miles

    Miles Guest

    Hmm...

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

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

    Yes, was the noob factor.

    Cheers,

    Miles...


    "Miles" <> wrote in message
    news:3ffaba36$0$22625$...
    > 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 !
    >
    >
    Miles, Jan 6, 2004
    #2
    1. Advertising

  3. In article <3ffaba36$0$22625$>,
    Miles <> 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.
    Mark Jason Dominus, Jan 6, 2004
    #3
  4. Miles

    Dave Weaver Guest

    On Wed, 7 Jan 2004 00:08:25 +1000, Miles <> 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.
    Dave Weaver, Jan 6, 2004
    #4
  5. Miles

    Erik Tank Guest

    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"
    <> 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" <> wrote in message
    >news:3ffaba36$0$22625$...
    >> 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 !
    >>
    >>

    >
    Erik Tank, Jan 6, 2004
    #5
  6. Erik Tank <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 6, 2004
    #6
  7. Miles

    Miles Guest

    Thanks for the replies,

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

    Cheers,

    Miles.


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

    >
    >
    Miles, Jan 6, 2004
    #7
  8. Miles

    Miles Guest

    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" <> wrote in message
    news:btekak$d4u$...
    >
    > In article <3ffaba36$0$22625$>,
    > Miles <> 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.
    >
    >
    >
    Miles, Jan 6, 2004
    #8
  9. Miles

    Uri Guttman Guest

    >>>>> "M" == Miles <> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Jan 7, 2004
    #9
  10. Miles

    Miles Guest

    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...
    Miles, Jan 7, 2004
    #10
  11. Miles

    Uri Guttman Guest

    >>>>> "M" == Miles <> writes:

    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Jan 7, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John

    empty/non-empty element

    John, Jul 15, 2003, in forum: XML
    Replies:
    1
    Views:
    1,009
    Klaus Johannes Rusch
    Jul 16, 2003
  2. Lukas
    Replies:
    3
    Views:
    782
    spiff
    Nov 10, 2005
  3. Marcia Hon

    Check if a directory is empty and empty it

    Marcia Hon, Feb 8, 2004, in forum: C Programming
    Replies:
    8
    Views:
    562
    Dave Thompson
    Feb 14, 2004
  4. Brian Roberts

    empty lists vs empty generators

    Brian Roberts, May 3, 2005, in forum: Python
    Replies:
    12
    Views:
    640
    Jeremy Bowers
    May 4, 2005
  5. ButlerDJIAM
    Replies:
    0
    Views:
    545
    ButlerDJIAM
    Nov 9, 2006
Loading...

Share This Page