I have a list of files with mmddyyyy_nnnnn
format. How do I find file with the highest number in postfix nnnnn
using perl?
Example: for these three files
11232014_00001
11232014_00002
11232014_00003
I want to return file name for 00003.
I have a list of files with mmddyyyy_nnnnn
format. How do I find file with the highest number in postfix nnnnn
using perl?
Example: for these three files
11232014_00001
11232014_00002
11232014_00003
I want to return file name for 00003.
Full sort as in ST is not efficient for larger lists when only looking for maximal value, thus reduce
from List::Util
core module might be optimal solution,
use List::Util 'reduce';
my @files = qw(11232014_00001 11242013_00002 11232012_00003);
my ($max) = reduce {
my ($aa, $bb) = map /(\d+)$/, $a,$b;
$aa > $bb ? $a : $b
}
@files;
Similar approach by using max_by
from List::UtilsBy
use List::UtilsBy 'max_by';
my @files = qw(11232014_00001 11242013_00002 11232012_00003);
my $max = max_by { ( /(\d+)$/ )[0] } @files;
I'd use a Schwartzian transform:
#!/usr/bin/perl
use Modern::Perl;
my @files = qw(11232014_00001 11242013_00002 11232012_00003);
my ($highest)=
map { $_->[0] }
sort { $b->[1] <=> $a->[1] }
map { [$_, /(\d+)$/] }
@files;
say $highest;
Output:
11232012_00003
I hate using sort
for obviously linear algorithm (you don't actually need to sort your list, you only want to find a maximum), so here is a solution using good old foreach
loop and no sorting at all:
use strict;
use Modern::Perl;
my @files = qw(11232014_00001 11242013_00002 11232012_00003 22222222_00001);
my ($max_value, $max);
foreach (@files) {
if (/_(\d+)$/ && (!defined $max_value || $1 > $max_value)) {
$max_value = $1;
$max = $_;
}
}
say $max;
Since you dealing with fixed width filenames, I'd suggest reformatting and sorting by alpha:
Outputs: