Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何在perl中将两个字符串转换为哈希?_String_Perl_Hash_Map_Split - Fatal编程技术网

String 如何在perl中将两个字符串转换为哈希?

String 如何在perl中将两个字符串转换为哈希?,string,perl,hash,map,split,String,Perl,Hash,Map,Split,我有两条线: 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 °

我有两条线:

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的一些变体,但得到了一些神秘的结果;-)

您可以使用:


这里有一个没有List::MoreUtils的详细解决方案

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";

my @time = map { (split /-/, $_)[0] } split m! / !, $wTime;
my @temp = split m! / !, $wTemp;

my %hash;
for (my $i=0; $i <= $#time; $i++) { # Iterate the times via their index...
  # This only works if we have an equal number of temps and times of course.
  $hash{$time[$i]} = $temp[$i];
}
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”;
我的@time=map{(split/-/,$([0]}split m!/$时间;
我的@temp=split m!/$wTemp;
我的%hash;
对于(my$i=0;$i另一种方式:

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";

my %h1;
@h1{$wTime=~/([\d:]+)-/g}=split(m! / !,$wTemp);
创建两个列表

my @wTimes = map /([^-]+)/, split qr{ / }, $wTime;
my @wTemps = split qr{ / }, $wTemp;
然后使用散列切片

my %hash;
@hash{@wTimes} = @wTemps;

谢谢你的回答。我忘记了:没有任何其他模块。欢迎使用SO。这是一个很好的第一个问题。如果你能展示一些你尝试过的
map
split
事情,那会更好。只有当人们指出你的错误时,你才能从中吸取教训。;-)正如我之前所说的:我已经尝试使用例如,
%hash=map{split/\s*\/\s*/,$\u}split/-/,$wTime来拆分$wTime但这会将每个时间串拆分为一个键。我也尝试过其他一些变体。我冒昧地将此添加到您的问题中。你也可以自己提问。这很有效,但并不完全有效!:-)最后一个时间刻度<代码> 23:00—00时< /COD>被忽略。<代码>您也可以考虑“代码> @哈希{@ Time} @ @ TEMP/<代码>,使用哈希切片分配。@ TLP将更为流行,但我将坚持这个解决方案。很遗憾,你不能在一行(即陈述)中完成它;)@Xaerxess,你当然可以<代码>我的%h1=do{my%h;@h{…}=…;%h}。有一些方法不使用
do
do
)永远不要低估Perl将所有内容放在一行的能力。。。
my @wTimes = map /([^-]+)/, split qr{ / }, $wTime;
my @wTemps = split qr{ / }, $wTemp;
my %hash;
@hash{@wTimes} = @wTemps;