perl如何将各个值分配给变量

时间:2015-08-21 11:13:58

标签: perl perl-hash

我正在尝试将哈希值分配给变量,但我无法将每个值分配给varIAble。请分享您的想法

say my %data = Dumper($data);   

$VAR1 = {
          'cnt' => 2000,
          'inc' => 30,
          'start' => 1440154820,
          'end' => 1440154860,
          'values' => [
                        '0.001234',
                        '0.001878',
                        '0.001849',

预期产出

$start = 0.001234:1440154820
$end = 0.001849:1440154860

嗨,我正试图从上面的哈希

中将这些值分配给变量,如下所示
$value1 = 0.001234:1440154820   # like $VAR1{values[0]:$VAR1{start}
$value2 = 0.001849:1440154860   # $VAR1{values[2]:$VAR1{end}

1 个答案:

答案 0 :(得分:0)

这是你要求的吗?

use strict;
use warnings;

my $data = {
  cnt    => 2000,
  inc    => 30,
  start  => 1440154820,
  end    => 1440154860,
  values => [0.001234, 0.001878, 0.001849],
};

my $start = join ':', $data->{values}[0], $data->{start};
my $end   = join ':', $data->{values}[2], $data->{end};

print "\$start = $start\n";
print "\$end   = $end\n";

输出

$start = 0.001234:1440154820
$end   = 0.001849:1440154860