Hi - How can I modify the code below to sort and output the numerials+alpha characters at the bottom of the output.txt file I'm writing to? See example output below of what I require. Code is slopppy. Just learning.

require 5.000; use warnings; use strict; use POSIX; my %tags = (); my $input = $ARGV[0]; my $output = $ARGV[1]; open (FILE, "< $input") or die "cannot open $input: $!\n"; while (my $tag = <FILE>) { $tag =~ m/<t id=(\d*)(.*)((\d*)([[:alpha:]]*))>/; if ($tag eq "\n") { # BLANK LINE } else { $tag =~ m/<t id=\((\d*)(.*)((\d*)([[:alpha:]]*))\)>/; $tags{sprintf("%04d%6s",$1 || 9999,$2)} = $tag; } } close NEWFILE; close FILE; open (NEWFILE, "> $output") or die "cannot open $output: $!\n"; foreach my $id ( sort keys %tags ) { print NEWFILE $tags{$id}; print $tags{$id}; } close NEWFILE; close FILE;

input.txt

<t id=2bc>Only the...</t>
<t id=1>Only the...</t>
<t id=12>Only the...</t>
<t id=21>Only the...</t>
<t id=1>Only the...</t>
<t id=1a>Only the...</t>
<t id=2>Only the...</t>
<t id=2>Only the...</t>
<t id=2>Only the...</t>
<t id=2>Only the...</t>
<t id=3>Only the...</t>
<t id=35>Only the...</t>
<t id=31>Only the...</t>
<t id=2b>Only the...</t>
<t id=4>Only the...</t>
<t id=42>Only the...</t>
<t id=5>Only the...</t>
<t id=51>Only the...</t>
<t id=2ac>Only the...</t>
<t id=52>Only the...</t>
<t id=6>Only the...</t>
<t id=7>Only the...</t>

Required results: output.txt

<t id=1>Only the...</t>
<t id=2>Only the...</t>
<t id=3>Only the...</t>
<t id=4>Only the...</t>
<t id=5>Only the...</t>
<t id=6>Only the...</t>
<t id=7>Only the...</t>
<t id=12>Only the...</t>
<t id=21>Only the...</t>
<t id=31>Only the...</t>
<t id=35>Only the...</t>
<t id=42>Only the...</t>
<t id=51>Only the...</t>
<t id=52>Only the...</t>
<t id=1a>Only the...</t>
<t id=2ac>Only the...</t>
<t id=2b>Only the...</t>
<t id=2bc>Only the...</t>

In reply to Sorting numerials first and then numerials with alpha characters last by VladP

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.