If I understand your question, you want the lines contained in @DB to be printed in sorted order. You didn't say which field the sorting should be based on, but I'll assume you want it based on the initial field. Your examples only show 4-digit numbers in the first field, but I'll assume that you could have 3-digit and/or 5-digit numbers as well, and you want the sort to be numeric. I think the part of the foreach loop that you are not showing is the part where you want to print the lines in sorted order. The answer to your question is that you need to apply the sort before going into the loop -- this could do it (if all my assumptions are correct):
foreach $rec ( sort { $a <=> $b } @DB ) { chomp $rec; ($num,$name,$email,$xnumber) = split /\#/, $rec; # print and do whatever else you want... }
The sort block uses numeric comparison; it will do the right thing with the strings in your example, no matter how many digits are in the first field. If you want to sort based on some other field in each line, you'll need to build a hash with the hash key being the field you are sorting on, and the value being the other fields in each line. That means you need one loop over @DB to split each line and store it in the hash; then you need a second loop to print out the data in sorted order, and this second loop begins with something like:
foreach my $key (sort keys %dbhash) { # print the data found in $dbhash{$key} }

In reply to Re: Sort Problem by graff
in thread Sort Problem 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.