Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

HASH Array advice

by Anonymous Monk
on Jan 24, 2006 at 11:33 UTC ( [id://525157]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks...

I just need to ask for a piece of advice.
Is it wise to use intergers for keys in a hash array?

here's an example of my array
%dict =( label => { 1 => 'label 1', 2 => 'label 2' } )

thanx all

Replies are listed 'Best First'.
Re: HASH Array advice
by reasonablekeith (Deacon) on Jan 24, 2006 at 11:43 UTC
    No real problem, but it should make you think about using an array in there instead....

    my %dict; $dict{'label'} = ['label 1', 'label 2']; print Dumper(\%dict);
    ---
    my name's not Keith, and I'm not reasonable.
      Thank you for your response...
      So I must change it to something like this?
      %dict =( label => ('label 1', 'label 2') )

      What's the pro's of doing it this way?

        That should be:

        %dict =( label => ['label 1', 'label 2'] )

        Using [ .. ] to create an anonymous array.

        The advantage is that array access is much faster than hash access.

        A good approch would be to code up both alternatives and benchmark them.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

        as said above, an array would be faster, but I mentioned this mainly in a "right tool for the job" type way.

        If you are accessing something based on a numeric index, you might as well be using an array. It's what they were designed for. :)

        ---
        my name's not Keith, and I'm not reasonable.
Re: HASH Array advice
by serf (Chaplain) on Jan 24, 2006 at 11:46 UTC
    There's no specific reason not to.

    If you are only making a small hash (with low numbers) you may find that an array is easier and more efficient *but* there are benefits in using a hash this way over using an array:

    Particularly if you have to use a very large index number in the array; I believe that an array is created with (empty where not defined) elements up to the size of the highest index number, so by using a large index number you are pre-allocating (and therefore wasting) a chunk of un-needed memory.

    On the other hand with an array you can just do:

    for my $element (@array)
    and get all the elements back in order (which may or may not be useful in the case you are looking at), whereas with a hash you would have to sort the keys:
    for my $key (sort keys %hash)
    or explicitly ask for them:
    for my $key (@array_of keys)
    (which obviously means using a hash *and* an array!) because a hash does not store the keys (or values) in numerical order.
Re: HASH Array advice
by holli (Abbot) on Jan 24, 2006 at 11:48 UTC
    That depends.

    Seriously, there is nothing wrong with it when that helps you to
    - solve your problem and
    - you are aware that keys returns the keys in an unpredictable order.


    holli, /regexed monk/
Re: HASH Array advice
by jonadab (Parson) on Jan 24, 2006 at 14:35 UTC
    Is it wise to use intergers for keys in a hash array?

    Wise? It's certainly okay, but it's not inherently better than using strings for the hash keys. In fact, the integers will be implicitely converted to strings anyway (i.e., 1 becomes '1' and 2 becomes '2'). This does not create any problems, though, because it's transparent and fully reversible (unlike, say, using references as hash keys, which creates problems in Perl5, although I think Perl6 is fixing that).

    So, feel free to use the numbers as your hash keys if it suits your purposes, but in a different situation if strings would suit your purposes better, then use those. TMTOWTDI.

Re: HASH Array advice
by vennirajan (Friar) on Jan 24, 2006 at 11:47 UTC

         It does't make any difference with arrays. Actualy, the use of hashes is we can use strings as indexes for the values.If you go for numbers as keys in hash means, its similar like using arrays. But, there may not be sequential present of indexed elements.



    Regards,
    S.Venni Rajan.
    "A Flair For Excellence."
                    -- BK Systems.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-18 16:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found