VladP has asked for the wisdom of the Perl Monks concerning the following question:
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>
|
|---|