Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Extracting single element from array to be printed in a one liner hash

by Lotus1 (Chaplain)
on May 20, 2014 at 13:55 UTC ( #1086790=note: print w/ replies, xml ) Need Help??


in reply to Extracting single element from array to be printed in a one liner hash

foreach $key (keys(%data)){ if($key{$data} eq $input){

It looks like you swapped your hash and iterator variable. $key{$data}: there are no hashes called key available. If you had used strict it would have given you an error about it.

Update: Since $data{$key} will always equal "movies" or "fruits" I think you meant to do this:

foreach $key ( keys(%data) ){ if($key eq $input){


Comment on Re: Extracting single element from array to be printed in a one liner hash
Select or Download Code
Re^2: Extracting single element from array to be printed in a one liner hash
by wildguess123 (Initiate) on May 20, 2014 at 14:02 UTC
    I got the following error when u changed the foreach loop to yours:
    Please enter a string apple Use of uninitialized value $data in hash element at p3task6.pl line 30 +, <STDIN> line 2. Use of uninitialized value in string eq at p3task6.pl line 30, <STDIN> + line 2. Use of uninitialized value $data in hash element at p3task6.pl line 30 +, <STDIN> line 2. Use of uninitialized value in string eq at p3task6.pl line 30, <STDIN> + line 2. Use of uninitialized value $data in hash element at p3task6.pl line 30 +, <STDIN> line 2. Use of uninitialized value in string eq at p3task6.pl line 30, <STDIN> + line 2. Use of uninitialized value $data in hash element at p3task6.pl line 30 +, <STDIN> line 2. Use of uninitialized value in string eq at p3task6.pl line 30, <STDIN> + line 2. Use of uninitialized value $data in hash element at p3task6.pl line 30 +, <STDIN> line 2. Use of uninitialized value in string eq at p3task6.pl line 30, <STDIN> + line 2. Use of uninitialized value $data in hash element at p3task6.pl line 30 +, <STDIN> line 2. Use of uninitialized value in string eq at p3task6.pl line 30, <STDIN> + line 2. Use of uninitialized value $data in hash element at p3task6.pl line 30 +, <STDIN> line 2. Use of uninitialized value in string eq at p3task6.pl line 30, <STDIN> + line 2. Use of uninitialized value $data in hash element at p3task6.pl line 30 +, <STDIN> line 2. Use of uninitialized value in string eq at p3task6.pl line 30, <STDIN> + line 2. Please enter a string

        The OP wanted to type apples for example and have it say "fruits". To do this you need:

        if ( $key eq $input ) { ... }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1086790]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (15)
As of 2015-07-07 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls