Question on use of Next, Redo and Last 
Author Message
 Question on use of Next, Redo and Last

I've been writing a program to parse through a bunch of mail messages
consisting of syserr logs from workstations that we manage on campus
here.  I used some code written by Larry Wall to parse out the machine
the error log came from and some other info, and then the body of the
message which contained the actuall log for that day.  I still don't
totally grok (or even understand!  :-) why the code works.  I've read
the camel book quite a few times, but the section on last, next, etc
is kinda weak without a really good contrived example.  Here is
Larry's code and then mine, just to give an example I can deal with.

Thanks!

                                        John

----------------------------larry code---------------------------------


$USER = $ENV{'USER'};
open(MAILBOX, "/usr/spool/mail/$USER")
    || die "cannot open mailbox for $USER: $!\n";

$/ = "";

while (<MAILBOX>) {
    next unless /^From /;
    next unless /\*\*Phone Message\*\*/;

    # Got one...

    $_ = <MAILBOX>;       # Assuming next paragraph contains this stuff:
    ($from) = /From:  +(.*)/;
    ($comp) = /Company: +(.*)/;
    ($phone) = /Phone: +(.*)/;
    print "F: $from ($phone). M: ";

    while (<MAILBOX>) {
        last if /^From /;
        tr/\n/ /;               # or maybe tr/\n/ /s
        print;
    }
    print "\n";
    redo if $_;                 # m{*filter*}equivalent of unget

Quote:
}

-----------------------my code-------------------------------------
#!/usr/local/bin/perl
#
#
# This program reads in a bunch of mail messages from the syserr demons and
# creates a summary report.  This saves my brain cells and fingers!
#
# fmterr.pl version 1.0  9/10/92
#

$syserr = "/usr1/john/.SYSERR";

$/ = "";      # to read in paragraphs of stuff
                # tried using "From ", but that just caught
                # the beginning headers AFTER they went through.
                # maybe redo would do the trick?

$\ = "\n";

open(SYSERR,$syserr) || die "Can't open $syserr: $!\n";

TOP:
while (<SYSERR>) {
    next unless /^From /;

    ($hostname) = join('',"--",/Subject: syserr on (\w+)\.*/,"--");
    ($date) = /Date: .*, (.*) \d\d.*/;
    ($num) = split(' ',$date,1);
    if ($num < 10) {
        $date = '0' . $date;
    }
    $host{$hostname . $date} = $hostname;  # I like being able to do this!
    $date{$hostname . $date} = $date;

    while (<SYSERR>) {
        last if /^From /;

        $tmp = $tmp . $_;
    }                          

    $data{$hostname . $date} = $tmp;
    undef($tmp);

    redo if $_;

Quote:
}                                

$~ = BODY;

foreach $key (sort keys %host) {
    split(/^/,$data{$key});
    undef($count);
    undef($tmp);

        if (/file: table is full/) {
            $count++;
        }
        else {
            $tmp = $tmp . $_;
        }
    }
    if (defined($count)) {
        $tmp = $tmp . " file: table was full $count times\n\n";
    }

    undef($data{$key});
    $data{$key} = $tmp;
    write;

Quote:
}

format BODY =

$host{$key},$date{$key}

$data{$key}
.

--
Youth of today!  Join me in a mass rally for traditional mental attitudes!
-------------------------------------------------------------------------------

John Stoffel     | 508-831-5512 (work)     | Worcester, MA  01609



Sat, 04 Mar 1995 01:34:43 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. next and redo

2. Why no "next/last/redo" with do?

3. : last and next

4. PERLFUNC: pos - find or set the offset for the last/next m//g search

5. PERLFUNC: pos - find or set the offset for the last/next m//g search

6. shift doesn't, NEXT-to-last value?

7. Question Number 4 - Last Question

8. next if C VS if (C) { next }

9. next if C VS if(C) { next }

10. Constructing a next/more feature for a list display using PERL (CGI)

11. Skipping to next file using angle operator

12. Using substition to remove the last occurance of a word

 

 
Powered by phpBB® Forum Software