Ok, I need some advise on good sorting modules/commands/techniques. In the interest of honesty, I will admit that I have not done as much research on my own before asking this question as I normally would, but there are some time restraints. Any and all help would be appreciated.
The problem domain: I have a text file, which could be looked at like as a table, or a really cheesy DB. Each line is an entry, and the fields are space-separated. I want to sort this file based upon one of the fields.
Each of these fields is formatted in the following manner:
- A string (any string)
- Followed by a two digit number
- Followed by another string (again, any string)
- Followed by either _$string1 or _$string2 (there are two possible endings for each entry. These endings are not related in any way to the strings from 1. and 3.)
In case you are wondering, I am not the one responsible for this craziness.
Now here is the interesting part: The sort doesn't key off alphabetical order, or anything sane like that. Oh, no. Instead, I must sort this file according to the following little rules, listed in order of precedence:
- Entries ending in "_$string1" come before entries ending in "_$string2".
- Those entries with a higher two-digit number must come before those with a lower number.
- Some of the entries don't follow the above format. These entries must come after any that do.
- The strings should be in otherwise alphabetical order (both the string before the two digit number and the string after it, with the string before it given priority).
Could someone give me a shove in the general direction I should be looking? Does perl have any built-in or module-based sorting tools more complex than the sort command? Do any of them come close to handling things even remotely this weird?
Some people drink from the fountain of knowledge, others just gargle.
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.