Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 avoiding work at the Monastery: (14)
As of 2014-09-23 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (231 votes), past polls