in reply to pushing hash values to an array
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;
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:1, 2, one, two, three 3, 4, 5, one, two, three
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__
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>
|
|---|