0

Any perl expert can help me to understand this block of perl code

  $a=18;
$b=55;
$c=16;
$d=88;
$mtk = {
    'A'     => [$a, $b],
     'M'   => [$c, $d]

    };

is this dictionary hold char and pair and how to access key and values thanks a lot

1

2 Answers 2

8

$a, $b, $c, and $d are scalars. $mtk is a reference to a hash of arrayrefs. You can access it like:

print $mtk->{A}[0]; ## 18

I'd suggest the book Learning Perl if you are just getting started and struggling with this code.

perldoc perlreftut

Sign up to request clarification or add additional context in comments.

Comments

1

This is a hash ref for array refs as values. Here is a traverse code below:

for my $key (sort keys %$mtk) {
    print "Current key is $key\n";
    for my $val (@{ $mtk->{$key} }) {
        print "... and one of value is $val\n";
    }
}

The output will be

Current key is A
... and one of value is 18
... and one of value is 55
Current key is M
... and one of value is 16
... and one of value is 88

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.