bananabread has asked for the wisdom of the Perl Monks concerning the following question:

I have an array of numbers, lets say my @numbers = (1, 2, 3, 4, 5);

And each number is associated with a colour. I run my program and get the answer $colour = 1. I want to print the colour that's associated with the answer that my loop comes up with.

I made a hash where my %colours = ( blue => "1", green => "2", red => "3", pink => "4", purple => "6" );

But how do I get it to actually print the colour associated with the number?

  • Comment on How to print specific word from array/hash?

Replies are listed 'Best First'.
Re: How to print specific word from array/hash?
by Laurent_R (Canon) on Oct 05, 2016 at 06:20 UTC
    If you already have the %colours hash that you describe, then the best is to reverse it, as shown by Athanasius.

    But if you build the hash for this purpose, then, the best is probably to build it the right way from the start:

    my %colours = ( 1 => "blue", 2 => "green", ... );

    Yet, if your indices are all small integers, you might as well use an array:

    my @colours = qw/ blue green red pink purple /;
    bearing in mind, though, that array subscripts start at 0, so that you might want to use a dummy value for array subscript 0 if you need your colors to start at index 1:
    my @colours = qw/ nil blue green red pink purple /;
    Update: removed some commas left in the qw// list. Thanks to the monks who pointed out this type.
Re: How to print specific word from array/hash?
by Athanasius (Archbishop) on Oct 05, 2016 at 03:36 UTC

    Hello bananabread, and welcome to the Monastery!

    You need a hash in which the keys are numbers and the values are the corresponding colours. If you already have the hash %colours as shown (and need to keep it for some other reason), you can create a reversed hash easily using Perl’s built-in reverse function. As an illustration:

    use strict; use warnings; use Data::Dump; my %colours = ( blue => "1", green => "2", red => "3", pink => "4", p +urple => "6" ); my %reverse_colours = reverse %colours; dd \%reverse_colours; printf "The colour corresponding to %d is %s\n", 1, $reverse_colours{1 +};

    Output:

    13:32 >perl 1703_SoPW.pl { 1 => "blue", 2 => "green", 3 => "red", 4 => "pink", 6 => "purple" } The colour corresponding to 1 is blue 13:32 >

    See “How do I look up a hash element by value?” in perlfaq4.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: How to print specific word from array/hash?
by johngg (Canon) on Oct 05, 2016 at 10:27 UTC

    Others have pointed out that your %colours hash would be much more suitable if it was reversed. However, you can still obtain the colour from the chosen number with the hash as it stands by using keys and grep.

    johngg@shiraz:~ > perl -Mstrict -Mwarnings -E ' my %colours = ( blue => q{1}, green => q{2}, red => q{3}, pink => q{4}, purple => q{5}, ); my @numbers = qw{ 1 2 3 4 5 }; my $pick = $numbers[ int rand @numbers ]; say qq{Number $pick chosen}; my( $colour ) = grep { $colours{ $_ } eq $pick } keys %colours; say qq{Colour is $colour};' Number 2 chosen Colour is green

    Of course, building the hash the right way in the first place is by far the best approach.

    Cheers,

    JohnGG

      And of course, this approach allows a number to represent more than one color, if this is necessary:

      c:\@Work\Perl\monks>perl -wMstrict -le "my $color = 7; ;; my %colors = qw(blue 1 green 7 red 3 pink 7 purple 6); ;; my @picked = grep { $colors{$_} eq $color } keys %colors; print qq{color(s) picked by $color: @picked}; " color(s) picked by 7: green pink


      Give a man a fish:  <%-{-{-{-<