in reply to foreach returning to start of array
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:
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 @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]; }
#!/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}; }
|
|---|