Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

pushing hash values to an array

by valavanp (Curate)
on Oct 16, 2006 at 11:14 UTC ( #578470=perlquestion: print w/replies, xml ) Need Help??
valavanp has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, when i push hash values in an array, it gets added. But when i push it through reference it is not getting added. I am not giving the full code, only a part of it. Here is the code which i used, please correct me where i am wrong:
#!/usr/local/bin/perl use strict; use warnings; %h=('1', 'one', '2', 'two', '3', 'three'); @arr=(1,2); foreach $key(keys %h) { push(@arr,$h{$key}); } print @arr; @arr=(3,4,5); foreach $key(keys %h) { push(@arr,$$h{$key}); print "@arr\n"; }

Replies are listed 'Best First'.
Re: pushing hash values to an array
by davorg (Chancellor) on Oct 16, 2006 at 11:24 UTC

    Perhaps you should try fixing the errors that you get when running this code. With "use strict" enabled, this code doesn't run at all because you haven't declared any of your variables.

    Once you've fixed those problems, you'll see that you are refering to a hash reference, $h which doesn't exist anywhere in your code.


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

Re: pushing hash values to an array
by blazar (Canon) on Oct 16, 2006 at 11:51 UTC
    #!/usr/local/bin/perl use strict; use warnings; %h=('1', 'one', '2', 'two', '3', 'three');

    Since you're under strict this won't even compile.


    Ditto! (But I won't insist further.)

    foreach $key(keys %h) { push(@arr,$h{$key}); } print @arr;

    Thus is correct.

    foreach $key(keys %h) { push(@arr,$$h{$key});

    This is not. Why do you expect $h{$key} to be a scalar reference?

    print "@arr\n"; }

    You probably want to print out of the loop. Be careful (also) when preparing test examples.

    All in all I suspect (but it's hard to tell from your description of the problem) you be in a situation in which you have $href=\%h or the like, and then

    push @arr, $href->{$key};
    is the way to go.
      I am getting the hash reference values from the database through the keys of the hash values. I have a record with 3 fields in my backend. When i am retrieving the record with a particular field I am getting displayed only the number of fields, not the contents of the field which is 1,13 in this case(which are the actual values of the field). I am using these values in an combox box of a CGI application web page for multiple selection of an item. Only when i am pushing that hash values it is not getting displayed. In other areas, like retrieving rows it works fine. Please suggest me how can i proceed. I thank you for your suggestions.

        I still have some problems parsing your text. I understand that English may not be your mother tongue, it's not mine either. I feel like suggesting you to concentrate on the (possibly ) few relevant points with your issue and ignore anything else that may contribute the the noise/signal ratio of your post.

        I am getting the hash reference values from the database through the keys of the hash values.

        Ok, you have a hashref, period. You want to push its values into an array, right? Then the suggestion I gave you in my other reply should be fine. Did you try it? Does it work? Any inconvenience with it? Or else you may directly dereference the hashref as a whole and combine the two mentioned functions in one sweep:

        push @array, values %$hashref;
        I have a record with 3 fields in my backend.


        hi valavanp,

        When you're not sure what sort of variable you have I find it useful to use Data::Dumper. When you know what is actually in it you have a better chance of extracting the data.

        Show us the output of Data::Dumper and we'll be able to offer more help.

Re: pushing hash values to an array
by Hofmator (Curate) on Oct 16, 2006 at 11:18 UTC
    In these kind of cases it helps a lot if you tell us what you expected these statements to print and what they did print indeed. In this way we can deduct what you wanted to achieve - I, for example, have no clue what kind of behaviour you wanted to achieve with your code.

    -- Hofmator

    Code written by Hofmator and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: pushing hash values to an array
by Anonymous Monk on Oct 16, 2006 at 11:38 UTC
    Well, you are "pushing it through a reference" indeed. However, since that reference doesn't exist, only undefined values will be pushed (things get added, you don't see them).

    Either strict or warnings would have given you hints on what's wrong with your code.

Re: pushing hash values to an array
by fenLisesi (Priest) on Oct 16, 2006 at 12:35 UTC
    I am not sure what you are trying to do there, but you can start by cleaning up your code. Here is a sample attempt: that prints:
    1, 2, one, two, three 3, 4, 5, one, two, three
    which is probably not what you wanted. I have the feeling that you want to print the spelling of the integers in the array, if they are available. If that is the case, then the following may give you some ideas: You may have presumed that once you define a hash named %h, then a scalar named $h is automatically created and its value set to \%h, but that is not the case. You have to do that yourself.

    Make sure your code compiles and runs before you post it. It appears that the use strict; and use warnings; bits were added as accessories-after-the-fact. HTH.

    Update: Added <readmore>

Re: pushing hash values to an array
by jwkrahn (Monsignor) on Oct 16, 2006 at 15:22 UTC
    foreach $key(keys %h) { push(@arr,$h{$key}); }
    Could more simply be written as:
    push @arr, values %h;
    If the values contain array references then:
    push @arr, map @$_, values %h;
    Or if the values contain hash references then:
    push @arr, map %$_, values %h;
    And if you don't know beforehand:
    push @arr, map ref() eq 'ARRAY' ? @$_ : ref() eq 'HASH' ? %$_ : $_, va +lues %h;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://578470]
Approved by Hofmator
and one hand claps...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-03-27 06:41 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (316 votes). Check out past polls.