in reply to How to print specific word from array/hash?
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, |
|
|---|