首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用perl获取最近创建的目录?

如何使用perl获取最近创建的目录?
EN

Stack Overflow用户
提问于 2019-05-23 17:23:06
回答 2查看 48关注 0票数 0

艾德:-我有以下目录

  • 2019-05-20_16-38-21
  • 2019-05-20_16-38-22
  • 2019-05-20_16-38-23
  • 2019-05-20_16-38-24

我需要使用perl选择2019-05-20_16-38-24这个目录作为变量

有可能吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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
票数 2
EN

Stack Overflow用户

发布于 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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56272026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档