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

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

Replies are listed 'Best First'.
Re:x4: Newbie Question - arrays
by mohadib_the_nasty (Initiate) on Jan 13, 2003 at 05:38 UTC
    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