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:
use strict; use warnings; my %spelling_of = ( 1 => 'one', 2 => 'two', 3 => 'three', ); my @integers = (1, 2); foreach my $key (sort keys %spelling_of) { push @integers, $spelling_of{ $key }; } printf qq(%s\n), join q(, ), @integers; @integers = (3, 4, 5); my $spelling_href = \%spelling_of; foreach my $key (sort keys %$spelling_href) { push @integers, $spelling_href->{ $key }; } printf qq(%s\n), join q(, ), @integers;
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:
use strict; use warnings; my %spelling_of = ( 1 => 'one', 2 => 'two', 3 => 'three', ); my @integers = (1, 2); my @spellings = (); foreach my $integer (@integers) { my $spelling = $spelling_of{ $integer }; push @spellings, (defined( $spelling ) ? $spelling : q(?)); } printf qq(%s => %s\n), join( q(, ), @integers), join( q(, ), @spellings), ; @integers = (3, 4, 5); @spellings = (); my $spelling_href = \%spelling_of; foreach my $integer (@integers) { my $spelling = $spelling_href->{ $integer }; push @spellings, (defined( $spelling ) ? $spelling : q(?)); } printf qq(%s => %s\n), join( q(, ), @integers), join( q(, ), @spellings), ; __END__
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>


In reply to Re: pushing hash values to an array by fenLisesi
in thread pushing hash values to an array by valavanp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.