A foreach loop will always start at the first element and run to the last
element.  If you use "redo" while in that loop it will reset it and start
over, and if you use last it will drop out of the loop.  You only ever need
to restart a loop if you are already in it.

Maybe you could offer some sample data?

Rob


-----Original Message-----
From: James Lum [mailto:[EMAIL PROTECTED]]
Sent: Thursday, December 20, 2001 4:00 PM
To: [EMAIL PROTECTED]
Subject: resetting foreach loops and @lists


hi,
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);
   }
   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});
  }
  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

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.

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?

thanks,
jim


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to