perlperl-hash

perl how to assign the individual values out to variable


I am trying to assign the hash values to variables, but I'm unable to get each value assigned to a varIAble. Please share your thoughts

say my %data = Dumper($data);   

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

expected output

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

Hi, I am trying to get these values assigned to variables as below from the above hash

$value1 = 0.001234:1440154820   # like $VAR1{values[0]:$VAR1{start}
$value2 = 0.001849:1440154860   # $VAR1{values[2]:$VAR1{end}

Solution

  • Is this what you're asking for?

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

    output

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