Ed:-我有以下目录
我需要使用perl选择2019-05-20_16-38-24此目录作为变量
有可能吗?
答案 0 :(得分:2)
如果您的目录命名不一致,那么这里是@choroba答案的替代方法。它使用File::stat(Perl随附的标准)和Schwartzian Transform对目录的创建日期进行排序:
#! /usr/bin/perl
use strict;
use warnings;
use File::stat;
my $parent = '.'; # parent directory you're searching
# Schwartzian Transform to sort directories by their creation time
# see https://en.wikipedia.org/wiki/Schwartzian_transform
my @dirs =
map { $_->[0] } # 3. extract the directory names from the sorted list
sort { $a->[1] <=> $b->[1] } # 2. sort the arrays on creation time
map { [ $_ => stat( $_ )->ctime ] } # 1. get the creation time for each directory
# and store it with the directory name in an array
glob( "$parent/*/" ); # 0. get the directories in the parent dir
my $most_recent = $dirs[-1]; # the last one is the most recent
答案 1 :(得分:1)
对于给定格式的时间戳,可以正常进行字符串比较,因此可以使用List::Util中的maxstr
。
#!/usr/bin/perl
use warnings;
use strict;
use List::Util qw{ maxstr };
my @dirs = qw(
2019-05-20_16-38-21
2019-05-20_16-38-22
2019-05-20_16-38-23
2019-05-20_16-38-24
);
my $most_recent = maxstr(@dirs);