use strict; use warnings; use Text::CSV; # I had to specify "XS" in the use line and in the call to new, below #use Text::CSV_XS; open my $fh, '<', 'filename.txt' or die "Cannot open filename.txt: $!\n"; my $parser = Text::CSV->new({ sep_char => '|' }); $parser->column_names($parser->getline($fh)); # $parser->getline_hr_all() returns a reference to an array; # which is easy enough to use, but the @{...} syntax unpacks # it to an array, which you might find convenient my @rows = @{$parser->getline_hr_all($fh)}; close $fh; # sort can take a block of code to specify how things should # be sorted, which in your case would be "by which columns" # use cmp for string sort; <=> for numeric sort, and # Unicode::Collate ( https://metacpan.org/pod/Unicode::Collate ) # for anything complex for my $row (sort { $a->{col_name} cmp $b->{col_name} } @rows) { ... } for my $row (sort { $a->{number} <=> $b->{number} } @rows) { ... }