我有一个数组
@array = ( 'Apr 11 21:14:25',
'Apr 11 21:10:10',
'Apr 11 21:09:10',
'Apr 11 21:07:10',
);
在这里,我要对数组中的时间戳进行映射:处理:数组中的第一个值应该与第二个值进行比较,以检查2分钟的时差吗?数组中的第二个值应与第三个值进行比较,并再次检查2分钟的时差。相同的过程应该继续
关于如何在perl中实现这一点的任何想法?
如果您不首先付出努力,我们真的不会仅仅给出答案。但这是午餐时间,我想解决一个简单的编程问题。
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Time::Piece;
my @times = (
'Apr 11 21:14:25',
'Apr 11 21:10:10',
'Apr 11 21:09:10',
'Apr 11 21:07:10',
);
# Need a year for this to make sense. Let's use
# the current one.
my $year = localtime->year;
# Convert the string dates to Time::Piece objects
my @timepieces = map {
Time::Piece->strptime("$year $_", '%Y %b %d %H:%M:%S')
} @times;
for (1 .. $#times) {
say "Comparing $times[$_ - 1] with $times[$_]";
# Use abs() so we don't care which is larger.
my $seconds = abs($timepieces[$_ - 1] - $timepieces[$_]);
if ($seconds == 120) {
say 'Exactly two minutes difference';
} elsif ($seconds > 120) {
say 'More than two minutes difference';
} else {
say 'Less than two minutes difference';
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句