in reply to Re: Newbie Question - arrays
in thread Newbie Question - arrays

I like this idea, and acutaaly tried it earlier. The thing
is that the blank lines in my mail spool test true. I dont
know if there is a unseen crl-left or what. When i use perl
to search for \n or \n\n it doesent find any. So i dont
know how to test for the blank line...thats why i do ...

if($array !~ /A-Za-z0-9/){

because

if($array){

finds truth in the blank lines ??
any ideas?

Thaks Again,
jd

Replies are listed 'Best First'.
Re: Re: Re: Newbie Question - arrays
by graff (Chancellor) on Jan 13, 2003 at 04:36 UTC
    ...the blank lines in my mail spool test true...

    In order to see if a line is "blank" in the sense that it contains nothing, or contains only whitespace, do this:

    if ( $array =~ /^\s*$/ ) { # true if line is empty or whitespace only .... }
    But looking at the whole problem... It will probably be easier if you can add a little bit to the script that creates the email in the first place, so that it causes the body of the message to begin with a constant, recognizable line that never occurs in the mail header (and is unlikely to be found in other, unrelated email messages that might happen to get sent to this mailbox); this way, you don't have to worry about making sure you can parse a whole mailbox file correctly. In other words, something like this to create the message:
    (echo SYSDATAMSG; uname -a; uptime; date) | mail -s uptime time@taproo +t.bz
    Now, when you process the contents of your mailbox, just look for the string "SYSDATAMSG", read the next three lines for those outputs (and make sure you document your code to specify how the messages are supposed to be created):
    open(MAIL, "$file") || die "cant open spool\n"; my @mail_lines = <MAIL>; close (MAIL); while ( @mail_lines ) { $_ = shift @mail_lines; if ( /^SYSDATAMSG$/ ) { # next three lines are needed $uname_data = shift @mail_lines; $uptime_data = shift @mail_lines; $time_stamp = shift @mail_lines; # (maybe you want to do things with those lines before/besides + printing) print $uname_data, $uptime_data, $time_stamp; } }
    Note that I'm opting to use a "while" loop, and taking stuff off the array until it's empty (so I don't have to count array indexes).
      Thanks all for the help! I really like the while loop
      that graff suggests. The 'WHILE' in combo with the 'SHIFT'
      is really the kind of controll I was looking for.

      Only problem now, I'll have to bring all new code to
      the level of ellegence shown by graff and others...catch22.

      For hack like me that can be tuff!
      Thanks Alot :)
      jd
Re: Re: Re: Newbie Question - arrays
by grantm (Parson) on Jan 13, 2003 at 09:31 UTC

    Blank lines evaluate to true because in your case the blank line string is actually a string containing one character: the newline character.

    When Perl reads in line-at-a-time mode (actually record-at-a-time mode) it reads up to and including the newline character (the default record separator). You can use chomp to remove the record separator:

    @array = map { chomp; $_ } <MAIL>;

    A handy way to check that a line is not blank is to check if it contains a non-whitespace character (newline is a whitespace character):

    if($line =~ /\S/) { # line is not blank

    Or to test for blank:

    if($line !~ /\S/) { # line is blank

    There's no need to anchor the regex with ^ and/or $ since we want it to match (and stop searching) on the first non-whitespace character it finds.