Hi Anirban,

first we have to be clear how you want to sort the lines.
Does 10-1 sort before 1-11 ?
Do you want numeric (use <=>) or alphabetic (use cmp) order?

> while (my $line = <$RFH>) {
>         chomp($line);
>         if ($line =~ m/.*?\-(\d+)\-(\d+).*/) {
>                 $sequence_no = "$1$2";
>         #       print "$sequence_no-- $line\n";
>                 $file_content_hash{$sequence_no}="$line";
>         }
> }

this will only work as long as the sequence numbers are unique. If two
different lines have the same sequence number, the latter will be discarded.

My suggestion would be

#!/usr/bin/env perl
use 5.010;
use warnings;
use strict;
use autodie;
my $file_name = "RXMOI_TRX_CMD";
my @lines;

open(my $fh, "<", $file_name);

while(<$fh>) {
    chomp;
    if (/^RXMOI:MO=RXOTRX-([0-9]+)-([0-9]+)/) {
        push @lines, [$_, $1, $2];
    }
    else {
        warn("line '$_' does not match");
    }
}

my @sorted = sort {
# if the numbers before the "-" are equal, compare the numbers after the "-"
$a->[1] <=> $b->[1] ? $a->[1] <=> $b->[1] : $a->[2] <=> $b->[2]
}
@lines;         

say $_->[0] for @sorted;

You might also take a look at the section "Sorting Efficiently" in the
Alpacca book.

All the best, Simon





-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to