我将建议将您的信息存储为哈希而不是数组:
my %times = (
"stuff" => "00:04",
"more stuff" => "02:59",
"extra stuff" => "00:00",
);
现在,您可以获取这些东西的时间:
my $stuff_var = "stuff";
print "It took $stuff $time{$stuff} to run\n";
如果您的数据在数组中,则可以使用m//
正则表达式对其进行解析:
my %times;
for my $time ( @arr ) {
$time =~ /(.*)\s+(.+?)/; # Parentheses mark stuff and time
my $stuff = $1;
my $time = $2;
$time{$stuff} = $time;
}
现在你有了一个$time
带有时间戳的哈希值。如果你想分开小时和分钟,你会使用相同的技术。我会做一个散列的散列:
my %times;
for my $item ( @arr ) {
$itme =~ /(.*)\s+(.+?)/; # Parentheses mark stuff and time
my $stuff = $1;
my $time = $2;
$time{$stuff}->{TIME} = $time;
$time =~ /(.*):(.*)/;
$time{$stuff}->{HOUR} = $1;
$time{$stuff}->{MINUTE} = $2;
}
现在$time{$stuff}->{TIME}
将是(HH:MM)
,而$time{$stuff}->{HOUR}
将是小时,$time{$stuff}->{MINUTES}
将是分钟。