Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found