I believe I understand your confusion. Output happens not according to what comes first in your code, but what is executed first in your code. When you loop over an array you will process each element in turn. So you get the first element's output, the second element's output, etc. This has everything to do with how your data is organized, not your code.

If you wish to reorder your output you need to reorder your data. For that you can collect data into data structures as you go through the array. Then process the data structures at the end.

Here are some further points of potential confusion that I see in your code.

Here is some code that might do something more like what you want.
my @account = qw( 34765-22333-333489-99867-22340-23456-3229 XLM8876 AMP7765 WQP22349 ); my @account_starting_with_digit; my @account_starting_without_digit; for my $checked (map {split /-/, $_} @account) { if ($checked =~ /^\d/) { push @account_starting_with_digit, $checked; } else { push @account_starting_without_digit, $checked; } } print "*" . (join " ", @account_starting_without_digit) . "*<br>\n"; print "-" . (join " ", @account_starting_with_digit) . "-<br>\n";
If you wish further help I strongly suggest that in addition to showing your code (which is a good step) you actually show the output you were hoping that your code would produce but didn't. This would greatly clarify what you want, particularly if it is very different from what your code does.

In reply to Re: Regular Exp. in foreach loop Help! by tilly
in thread Regular Exp. in foreach loop Help! by Anonymous Monk

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.