Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: pushing hash values to an array

by blazar (Canon)
on Oct 16, 2006 at 11:51 UTC ( #578477=note: print w/replies, xml ) Need Help??

in reply to pushing hash values to an array

#!/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.

Replies are listed 'Best First'.
Re^2: pushing hash values to an array
by valavanp (Curate) on Oct 16, 2006 at 12:13 UTC
    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://578477]
[Discipulus]: but is really necessary to support these ancient versions? from 5.14 onward is not enough?
[Discipulus]: yes marto I understood
[Corion]: Discipulus: I'm slowly migrating my code to require 5.8.x ;) Most of my code works on 5.6, but Filter::signatures requires 5.10 I think
[Corion]: 5.14 is also a good target, widely available and fairly stable. Also, over five years old, which doesn't push people into a needless upgrade cycle
[Discipulus]: yes I saw your efforts Corion but the pain is worth?
[Corion]: Discipulus: IMO it's not that much pain, but most of the systems I use have a Perl 5.14 or newer on them nowadays
[marto]: the question is more, is Padre still the way new users should be directed?
[Discipulus]: ah ah I have all 5.26 atm but I play alone..
[Corion]: Except this RHEL box, which comes with Perl 5.10.1 , but there I installed my own 5.20.3 for the real Perl programs we run ;)
[Corion]: marto: I used Padre for some time but then switched to Notepad++, since all the funky features I wanted to put into Padre couldn't be made to work, and/or I lost interest :)

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (10)
As of 2018-06-25 08:49 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.