gpt4 book ai didi

string - 如何将两个字符串转换为 perl 中的散列?

转载 作者:行者123 更新时间:2023-12-02 08:42:47 24 4
gpt4 key购买 nike

我有两个字符串:

my $wTime = "00:00-06:00 / 06:00-09:00 / 09:00-17:00 / 17:00-23:00 / 23:00-00:00";
my $wTemp = "17.0 °C / 21.0 °C / 17.0 °C / 21.0 °C / 17.0 °C";

我想将这些字符串连接到一个散列中,其中每个时间刻度的第一部分是一个键,例如:

$hash = (
"00:00" => "17.0 °C",
"06:00" => "21.0 °C",
"09:00" => "17.0 °C",
"17:00" => "21.0 °C",
"23:00" => "17.0 °C"
);

我尝试了 map 和 split 的一些变体,但我得到了一些神秘的结果;-)

%hash = map {split /\s*\/\s*/, $_ } split /-/, $wTime;

最佳答案

您可以使用 List::MoreUtils zip / mesh function :

my @time_ranges = split ' / ', $wTime;
my @times = map { (split '-', $_)[0] } @time_ranges;
my @temps = split ' / ', $wTemp;

use List::MoreUtils qw(zip);
my %hash = zip @times, @temps;

关于string - 如何将两个字符串转换为 perl 中的散列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15090879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com