in reply to stoping and restarting a loop
Here's one way that you should be able to adapt for your needs.
#! perl -sw use strict; my @data = <DATA>; my ($type3total, $type4total) = (0,0); my $state = ''; for (@data) { chomp; my ($part1, $part2) = split; if ($state eq "type4" && $part1 eq "3") { process( $type3total, $type4total ); ($type3total, $type4total) = (0,0); } $state = "type$part1"; $type3total += $part2 if ($part1 == 3); $type4total += $part2 if ($part1 == 4); die "array contains a type 7 record\n" if $part1 == 7; } process( $type3total, $type4total ); sub process { my ($type3, $type4) = (shift, shift); print "Processing type 3 total of $type3 and a type 4 total of $ty +pe4\n"; return; } __DATA__ 3 200 4 50 3 100 3 100 3 100 4 50 4 25
Gives:
C:\test>196448 3 - 200 4 - 50 3 - 100 Processing type 3 total of 200 and a type 4 total of 50 3 - 100 3 - 100 4 - 50 4 - 25 Processing type 3 total of 300 and a type 4 total of 75
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Stopping and restarting a loop
by tadman (Prior) on Sep 10, 2002 at 03:55 UTC | |
by BrowserUk (Patriarch) on Sep 10, 2002 at 04:14 UTC | |
by tadman (Prior) on Sep 10, 2002 at 06:47 UTC | |
by BrowserUk (Patriarch) on Sep 10, 2002 at 08:37 UTC | |
by tadman (Prior) on Sep 10, 2002 at 15:46 UTC | |
|