Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How can I push a hash onto an array?

by blogan (Monk)
on Nov 15, 2000 at 02:25 UTC ( [id://41655]=perlquestion: print w/replies, xml ) Need Help??

blogan has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

This doesn't work:
my %items = ( foo => 4 ); push @kung, %item;

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Pushing hashes onto arrays
by jptxs (Curate) on Nov 15, 2000 at 07:11 UTC

    This is a job for references. See perlman:perlref for the whole story. The abridged version is that perl will let you pass around "pointers" to other structures. These are references. Basically they let you have a variable, a scalar specifically, which points to some other 'thing' (read very technical term :). That way you can do things just like you want to - build complex data structures (not to mention objects, anonymous data structures, and other wonderful beasties).

    The way it works is you tell perl that some new scalar is now assigned the 'location' of another data structure. The example is a bit clearer than the language:

    use strict; # or DIE : ) use warnings; # or SUFFER : ) my %hash = ( one => 'this', two => 'that' ); my $ref = \%hash; # the \ tells perl to store a reference # then you can use the reference like you would the hash by DEreferenc +ing it print "$ref->{one}"; # prints 'this'

    The \ in front of %hash is what clues perl in that $reference should be a reference to that hash. The -> is the de-referencer. Read it like get $ref and when you see that it is a reference to something else follow (arrow, get it? :) that reference to the 'thing' referred to, where, since you used the {} (read hashy things), you find a hash and get that key's value. So your code becomes:

    #!/usr/bin/perl -w # -w does same as use warnings above, but for older + perls use strict; my (%item, @kung); $item{foo} = "5"; $item{bar} = "7"; push @kung, \%item; # note the slash print "Kungfoo is ->", $kung[0]{foo}, "<-\n";

    Note that the way you referred (ALL puns intended) to your value was just fine. You could also have used $kung[0]->{foo}. Perl gives you the convenience of leaving out the arrows sometimes. Did I say see perlman:perlref for the details yet?

    Originally posted as a Categorized Answer.

Re: How can I push a hash onto an array?
by slayven (Pilgrim) on Nov 15, 2000 at 05:02 UTC
Re: How can I push a hash onto an array?
by davorg (Chancellor) on Nov 15, 2000 at 14:47 UTC
    Whilst perlref is the definitive place to go for information about references, there are a couple of better places for beginners to go. I recommend perlreftut and perldsc.
Re: Pushing hashes onto arrays
by jptxs (Curate) on Nov 15, 2000 at 02:43 UTC
    What you need to do is push a reference to the hash into the array. see perlman:perlref for details on references, but basically a reference is something that points to something rather than being the thing. If you know C, then it's a pointer that does what you always thought pointers should : ) If not, than it is a scalar which holds a value that points to where perl can find something else like a hash.

    references can be simple like:

    use strict; # or DIE : ) use warnings; # or SUFFER : ) my %hash = { one => 'this', two => 'that' }; my $reference = \%hash #the \ tells perl to store a reference # then you can use the reference like you would the hash by DEreferenc +ing it print "$reference->{one}"; # prints this

    so in your case you would actually do sometihng like:

    push @kung, \%item; print "Kungfoo is ->", $kung[0]{foo}, "<-\n";

    Originally posted as a Categorized Answer.

Re: Pushing hashes onto arrays
by merlyn (Sage) on Nov 15, 2000 at 03:34 UTC
    push @kung, \%item;

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-07-17 13:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.