艾德:-我有以下目录
我需要使用perl选择2019-05-20_16-38-24这个目录作为变量
有可能吗?
发布于 2019-05-23 22:52:32
如果您的目录命名不一致,这里有一个替代@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
发布于 2019-05-23 17:31:49
对于给定格式的时间戳,正常的字符串比较有效,因此您可以使用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);
https://stackoverflow.com/questions/56272026
复制相似问题