Monday, March 14, 2016

Sort file by lines lengths

Often I need to sort file by length of lines and always I can't remember code which is doing this.

my @elements;

while(my $line = <>){
    push @elements, $line;
}

@sorted = sort { length $a <=> length $b } @elements;

foreach my $l (@sorted){
   print($l);
}

1 comment: