in reply to Sorting numerials first and then numerials with alpha characters last
Code is slopppy. Just learning.
That's fine - I will leave most of your code as it stands and put a Schwartzian Transform into your sort line. This produces your specified output - let's hope that's representative. :-)
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 FILE; open (NEWFILE, "> $output") or die "cannot open $output: $!\n"; foreach my $id ( map { $_ = $_->[2] } sort { $a->[1] cmp $b->[1] || $a->[0] <=> $b->[0] } map { /(\d+)([a-z]*)/; $_ = [$1, $2, $_] } keys %tags ) { print NEWFILE $tags{$id}; print $tags{$id}; } close NEWFILE;
🦛
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting numerials first and then numerials with alpha characters last (named captures)
by LanX (Saint) on Oct 29, 2021 at 19:14 UTC |