James Lum wrote:
>
> how do i reset a foreach loop so that it starts at the top each time?
>
> a sample of my code looks like:
>
> foreach $zrec (@file) # file with agent and city
> { chomp($zrec);
> foreach $zkey (@template) # put agent city data into hash
> { $hash{$zkey},$zrec)=split(/\//,$zrec,2);
^ ^
Missing left parentheses
> }
> foreach $zrec2 (@list) # table with city and zipcode
> { chomp($zrec2);
> foreach $zkey2 (@template2) # put city zipcode data into hash
> { ($hash2{$zkey2}, $zrec2)=split(/\//,$zrec2,2);
> }
> last if ($hash{city} eq $hash2{city});
^^^^ ^^^^
Bare words as hash keys.
> }
> print "agent='$hash{agent}' city='$hash{city}'
> zipcode='$hash2{zipcode}'\n";
> }
>
> the code looks like it should work ... but the 2nd and 3rd foreach loops
I can't really tell from your code what exactly you are trying to do.
> do not behave as expected. :(
> i have read about 'reset' and 'continue blocks' but have not been able
> to make them reset the foreach loops and @list processing to work as
> i would like them to.
Have you read the perlsyn manual page?
perldoc perlsyn
> two questions:
> 1. EXACTLY how should the code be written to ALWAYS start a foreach
> loop at the beginning?
> 2. EXACTLY how should the code be written so that a foreach loop on
> a @list ALWAYS starts at the beginning of a @list?
Try this example to see if it does what you want:
#!/usr/bin/perl -w
use strict;
my $count;
START:
for my $var ( 1 .. 4 ) {
print "$var ";
last if ++$count > 20;
goto START if $_ == 3;
}
print "\n";
__END__
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]