Howdy,
I've got a data file that's resisting all of my attempts to sort it correctly. Unfortunately, my skill with Perl is such that even a simple sort is still a challenge, much less a numerical sort on data that's embedded in text strings, thusly; (three space seperator)
953-FMT-FMT 954-FMT-FMT BLOGO93-FMT-FMT 955-FMT-FMT BOXSTART2- +FMT-FMT BOXEND2-FMT-FMT 956-FMT-FMT 413-FMT-FMT DATE1-FMT-FMT + 414-FMT-FMT 415-FMT-FMT 416-FMT-FMT BLOGO107-FMT-FMT 417-F +MT-FMT 419-FMT-FMT 418-FMT-FMT BLOGO63-FMT-FMT 393-FMT-FMT +394-FMT-FMT BLOGO75-FMT-FMT 420-FMT-FMT 421-FMT-FMT 395-FMT-F +MT
As you can see, there are 3 formats that I need ordered. Those that are ddd-FMT-FMT need a numerical sort. Those that are BLOGOddd-FMT-FMT need to be sorted seperately, but also numerically. Everything else can be sorted normally, but I'm going to run the output through a 20 year old ATEX system, so I need to retain the -FMT-FMT and BLOGO tags.
I've read the sort docs, to no avail. So far, my (somewhat feeble) attempts to get this done have all been variations on reading the data into an array and trying various sort and s/// combinations. Most of the code I've tried looks something like this:
#!/usr/bin/perl -w open (FH1, "input.txt") || die "Couldn't open input.txt"; $/ = " "; @records = <FH1>; open (FH2, ">>output.txt") || die "Couldn't open output.txt"; foreach my $record (sort {$a cmp $b} @records) { print FH2 "$record\n"; }

Am I approaching this all wrong? Should I be looking at split or map maybe? The prognosis for me figuring this one out anytime soon seems slim, so any help/guidance would be greatly appreciated.

a fronte praecipitium a tergo lupi

~scribe d4vis
#!/usr/bin/fnord


In reply to sort on numbers embedded in text by d4vis

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.