Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Should the element contains in an array/hash be scales?

Reply
Thread Tools

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

 
 
Jking
Guest
Posts: n/a
 
      08-24-2008
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


 
Reply With Quote
 
 
 
 
Michael Carman
Guest
Posts: n/a
 
      08-24-2008
Jking wrote:
> 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
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      08-24-2008

Quoth "Jking" <(E-Mail Removed)>:
>
> 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

--
Although few may originate a policy, we are all able to judge it.
Pericles of Athens, c.430 B.C.
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      08-24-2008
"Jking" <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Why the name of a file that contains java code should be same as the name of the class that is contained init? corejavagroups Java 6 11-21-2005 01:10 AM
select only the first node from a for-each loop that contains a specific element martin.tschofen@gmail.com XML 2 05-18-2005 11:53 AM
Regex problem, match if line contains <a>, unless it also contains <b> James Dyer Perl 5 02-20-2004 12:29 PM
Schema: specify that element contains XML snippet ian mayo XML 2 08-11-2003 12:26 PM



Advertisments