R
R
ReD2017-05-18 19:17:35
Perl
ReD, 2017-05-18 19:17:35

Why does a syntax error occur when trying to initialize a hash using a variable?

When trying to initialize the hash like this

$h{a} = "1";
$h{b} = "2";

An error occurs:
syntax error at p.pl line 93, near "$h{"
syntax error at p.pl line 94, near "$h{"

And if so:
$h->{a} = "1";
$h->{b} = "2";

then Perl doesn't swear.
Hence the question, when to initialize a hash using a variable, and when through a pointer to a hash?
Explain, please!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Saboteur, 2017-05-18
@trinitr0

$a="index1";

$h{$a}="1";
$h{"index2"}="2";

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question