Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: append values to a same key in hash

by jethro (Monsignor)
on Jul 06, 2009 at 09:26 UTC ( [id://777499]=note: print w/replies, xml ) Need Help??


in reply to append values to a same key in hash

$h{6077.1}->{xmltype}= '000';

You don't really append to a hash (since there is no ordered row or queue where you can append behind) but just insert values. To answer your probable next question too, if you want to test if a value already exists in the hash, you can use the function exists()

if (exists($h{6077.1}->{xmltype}) { ... }

UPDATE: changed two ] to }, stupid typing error found thanks to AnomalousMonk

Replies are listed 'Best First'.
Re^2: append values to a same key in hash
by ELISHEVA (Prior) on Jul 06, 2009 at 10:08 UTC

    Just be aware that exists($h{a}->{b}) will autovivify the first hash key component ({a}):

    use strict; use warnings; my %h; unless (exists($h{foo}{bar})) { print "\$h{foo}{bar} exists: no\n"; print "\$h{foo} exists: ", exists($h{foo})?'yes':'no', "\n"; } #outputs # $h{foo}{bar} exists: no # $h{foo} exists: yes

    If you want to be extra careful not to create hash keys, you'll have to use if (exists($h{a} and exists($h{a}->{b}) instead. and (or &&) short circuits so you'll never reach the second test if the first part of the key is non-existent.

    Best, beth

Re^2: append values to a same key in hash
by perldesire (Scribe) on Jul 06, 2009 at 09:40 UTC
    @shan emails,
    you can use hash of hash data structure.
Re^2: append values to a same key in hash
by shan_emails (Beadle) on Jul 06, 2009 at 10:03 UTC
    Thank you,

    it works fine.

    Thanks for your response.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-29 10:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found