Think it through. What exactly are you sorting on? If you want to sort on the first value, then the second, then the third, and sort asciibetically, then just do a sort on @DB. However, if you want to sort
- On the second value, then the fourth value, then the first, etc
- Each value differently, like the first value numerically, the second asciibetically, etc
then, you have to split each line up into its component parts. Something like the following will help:
my @records =
sort {
$a->[0] <=> $b->[0]
||
$a->[1] cmp $b->[1]
||
$a->[2] cmp $b->[2]
||
$a->[3] <=> $b->[3]
} map {
[ split '#', $_ ]
} map {
chomp;
$_
} @DB;
You have to read that code from bottom to top.
- You have all the lines in @DB
- Take each line and perform a chomp on it. Since chomp() returns the character(s) removed (if any), you need to explicitly return $_
- Create an array reference containing the values in the record (using split(), just like you were)
- Sort those records based on the elements of the record. <=> is a numeric sort. cmp is an asciibetical sort.
When it's all done, @records will be an array containing array references. If you need more explanation, please ask.
------
We are the carpenters and bricklayers of the Information Age.
Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.