Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Using an array in a hash

by btrott (Parson)
on Aug 08, 2000 at 02:50 UTC ( [id://26688]=note: print w/replies, xml ) Need Help??


in reply to Using an array in a hash

Your hash creation should look like this:
%LdnLine = ($j => \@check2);
The value must be a reference, not an actual array.

To answer your second question--why does it look right when you print out the entire hash--that's because your array is being treated as a list to initialize the hash. It's not treated as an array value to your key $j; the values in @check2 are used as both keys and values in your hash. This is more clear if you do this:

use Data::Dumper; print Dumper \%LdnLine;
With my dummy values for @check2 and $j this gave me
$VAR1 = { 1 => 'foo', 'bar' => 'baz' };
Which, I'll bet, is not what you were expecting.

Replies are listed 'Best First'.
RE: Re: Using an array in a hash
by maverick (Curate) on Aug 08, 2000 at 04:36 UTC
    Just a bit of elaboration on an excellent post.

    The keys and values in hashes can only be scalars, you can't directly store a array as a value. But, you can store a refrence to a array in a hash (a reference is a scalar) which is what btrott's code does.

    When you want to make a reference to another variable use the \ operator.

    my $ref_to_array = \@an_array; my $ref_to_hash = \%a_hash; my $ref_to_scalar = \$a_scalar;
    Or if you don't want to make a variable and then make a reference, you can initialize them this way.
    my $ref_to_array = [1,2,3,4]; my $ref_to_hash = { 'key1' => 'val1', 'key2' => 'val2' }; my $ref_to_scalar = \"some string\n";
    The contents can be retrieved like:
    print $ref_to_array->[0]; print $ref_to_hash->{'some_key'}; print ${$ref_to_scalar};
    All this stuff is explained in the o'reily (sp?) books in great detail. I'd suggest getting them. :)

    /\/\averick

      The keys and values in hashes can only be scalars

      And the keys can only be strings, not just any old scalar. A nit to pick, but an important one (which you probably already knew but that some readers may not have).

              - tye (but my friends call me "Tye")
RE: Re: Using an array in a hash
by BlaisePascal (Monk) on Aug 08, 2000 at 04:51 UTC
    To elaborate on that second part...

    When you say %hash = (a => "foo", b => "bar") perl treats the "=>" operator as a synonym for ",", so this really looks like %hash = (a,"foo",b,"bar"). Perl knows to treat a list assigned to a hash as key/value pairs, so this works right.

    In your example, where you tried %hash = (a => (foo,bar, baz)), perl treats the right hand side as (a,(foo,bar,baz)), which is "flattened" into (a,foo,bar,baz), which is then assigned to %hash, yielding %hash{a} = "foo" and %hash{bar} = "baz", which isn't what you wanted.

    The suggestions to use references will do what you want.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://26688]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2025-01-14 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (42 votes). Check out past polls.