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}; }

In reply to Re: foreach returning to start of array by mrbbking
in thread foreach returning to start of array by chrisbarton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.