Should the element contains in an array/hash be scales?

J

Jking

hi, guys,

Should the element contains in an array/hash be a scale, so that I can only
put references pointing to arrays/hashes in them?

In other words, is the assignment legal?

@a=(0,1);
%hi=( "hi"=>@a );

Thank you.

Justin
08/24
 
M

Michael Carman

Jking said:
Should the element contains in an array/hash be a scale, so that I can only
put references pointing to arrays/hashes in them?

Yes. Array elements and hash values must be a scalar (not "scale").
References are scalars, so they can be stored in an array or hash.
In other words, is the assignment legal?

@a=(0,1);
%hi=( "hi"=>@a );

That depends on what you mean by "legal." The syntax itself is valid.
(You can initialize a hash with an array.) It probably doesn't do what
you want, though. To perl, this looks like

%h = (hi => 0, 1)

Hash initializations should have an even number of elements (i.e. pairs
of keys & values). If you run this with warnings enabled you'll get

Odd number of elements in hash assignment...

because perl sees the key 'hi' with a value of 0 and the key '1' with no
corresponding value. What you (presumably) want is this:

%h = (hi => \@a);

perldoc perlreftut for a crash course in using references.

-mjc
 
B

Ben Morrow

Quoth "Jking said:
Should the element contains in an array/hash be a scale, so that I can only
put references pointing to arrays/hashes in them?

I presume you mean 'scalar'? Yes, that is correct: arrays and hashes
only contain scalars, so multi-level structures have to be built using
(explicit) references, unlike some other languages where the references
are implicit.
In other words, is the assignment legal?

@a=(0,1);
%hi=( "hi"=>@a );

It is, but it may not mean what you think. It is equivalent to

%hi = ("hi" => 0, 1);

which is equivalent to

%hi = ("hi" => 0, 1 => undef);

since hash assignment requires an even-sized list. It will create two
hash elements, "hi" with a value of 0 and "1" with a value of undef. If
you want a single, array-(reference-)valued element, you need

%hi = ("hi" => \@a);

See perldoc perlreftut and perldoc perldsc.

Ben
 
J

Jürgen Exner

Jking said:
Should the element contains in an array/hash be a scale, so that I can only

I suppose you mean scalar? A scale is something that fish have on their
skin.
put references pointing to arrays/hashes in them?
Yes.

In other words, is the assignment legal?

@a=(0,1);
%hi=( "hi"=>@a );

Yes, it is legitimate, but it probably doesn't do what you expect it to
do.

You can assign arrays to hashes in which case the array elements are
interpreted as key-value pairs. Furthermore the '=>' is just the same as
a regular comma. So that has assignment can be rewritten as
%hi = ('hi', (0, 1));
which is flattened to
%hi = ('hi', 0, 1);
which will assign 0 to $hi{hi} and yield a warning about odd number of
elements because there is no value specified for $hi{1}.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top