#! /usr/bin/env perl use Modern::Perl; use strict; use warnings; use feature qw( say state ); # Perl 5.10.0 or greater my @inventory = split /\R/, "YEAR MAKE MODEL PRICE 2003 abc rel 999 1999 hds sdf 100 2010 kls pol 1400"; my %columns = map { state $c = 0; lc, $c++ } map { split } (my $header = shift @inventory); print "sort by? "; chomp(my $lookfor = lc ); die "bad sort criteria" unless exists $columns{$lookfor}; my $sortcol = $columns{$lookfor}; say for $header, sort { if (grep { /\D/ } (split ' ', $a)[$sortcol], (split ' ', $b)[$sortcol]) { (split ' ', $a)[$sortcol] cmp (split ' ', $b)[$sortcol] } else { (split ' ', $a)[$sortcol] <=> (split ' ', $b)[$sortcol] } } @inventory;