【发布时间】:2020-12-01 02:30:41
【问题描述】:
我有一个哈希值,其中包含 unixtime 作为键和卷作为值。
我需要从体积小于threshold 值(我在开头定义的阈值)的哈希中获取 unixtime 和体积对,并且该对出现在哈希的第一位。
下面是我的脚本:
use strict;
use warnings;
use Data::Dumper;
use List::Util qw(reduce);
use POSIX qw( strftime );
my $threshold = 20;
my %hash = (
'1596561300' => '19',
'1596561306' => '12',
'1596561312' => '17',
'1596561318' => '20',
'1596561324' => '23',
'1596561330' => '11',
'1596561336' => '16',
'1596561342' => '15',
'1596561348' => '13',
'1596561354' => '17'
);
my $key = reduce { $hash{$a} <= $hash{$b} ? $a : $b } keys %hash;
my $val = $hash{$key};
$key = strftime("%Y-%m-%d %H:%M:%S", localtime($key));
print "Key=>$key :: Value=>$val\n";
在上面的脚本中,我能够从散列的所有值(卷)中获得最小散列值的 unixtime 卷。即,
Key=>2020-08-04 18:15:30 :: Value=>11
但我需要获取小于第一个/最小哈希键中出现的阈值的值。
对于上面的例子,它应该获取 ('1596561300' => '19') 即:
Key=>2020-08-04 18:15:00 :: Value=>19
我怎样才能得到它? TIA。
【问题讨论】:
-
您能否告诉您是否有两个条目(例如 17 值)
'1596561300' => '19',等于'2020-08-04 22:45:00' => 19,例如'1596561354' => '19'等于'2020-08-04 22:45:54' => 19在这种情况下您是哪个值会考虑吗? -
@amitbhosale :我应该考虑首先发生的那个。即
'2020-08-04 22:45:00 => 19'.