in reply to foreach returning to start of array

The problem is that foreach doesn't set any kind of iterator (that I know of, anyhow) - it just knows where it is in the array, and knows how to get to the next element in it.

If you want to use the for syntax, though, and you know your arrays have the same number of elements, and that they're in the correct order, you can use something like this:

#!/usr/bin/perl -w use strict; my $myemail = qw( chrisbarton@perlmonks.com ); my @emails = qw( firstemailaddress@com secondemailaddress@com); my @requests = ( "first lot of info\n------------------------------------------\n", "second lot of info\n------------------------------------------\n" ); for( my $i=0; $i<= $#emails; $i++ ){ print "To: $emails[$i]\n"; print "From: $myemail\n"; print "Subject: test\n\n"; print $requests[$i]; }
Mask's suggestion, though saves you the trouble of keeping your arrays synchronized, and keeps your code more Perlish:
#!/usr/bin/perl -w use strict; my $myemail = qw( chrisbarton@perlmonks.com ); my %requests = ( 'firstemailaddress@com' => "first lot of info\n------------------------------------------\n", 'secondemailaddress@com' => "second lot of info\n------------------------------------------\n" ); foreach my $email (keys %requests) { print "To: $email\n"; print "From: $myemail\n"; print "Subject: test\n\n"; print $requests{$email}; }