Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by wildguess123 (Initiate)
on May 20, 2014 at 13:35 UTC ( [id://1086785]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all. I have the following code, #!/usr/bin/perl -w

open(FILEHANDLE,'movies.txt');

@array2 =<FILEHANDLE>;

close FILEHANDLE;

open(FILEHANDLE2,'fruits.txt');

@array3 =<FILEHANDLE2>;

close FILEHANDLE2;

foreach $movie (@array2) {

chomp($movie);

$data {$movie} = "movies";

}

foreach $fruits (@array3) {

chomp($fruits);

$data {$fruits} = "fruits";

}

do{

print "Please enter a string \n";

chomp($input = <STDIN>);

foreach $key (keys(%data)){

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

print "$input is a $data{$key}\n";

}

}

} while ($input ne 0); The elements in movies.txt is the following:

the blind side

iron man

star trek

gi joe The elements in fruits.txt is the following:

orange

apple

pear

water melon

This is what I want to achieve. For example, when the user enters apples, It would be printed : apples is a fruits, and thats it. Another example, when I enter gi joe,it would print: gi joe is a movie. Now , I am facing the following problems, when I enter apples when the program ask the user to enter the string, it would not appear anything and would prompt us to enter again. How should I edit my code to fulfill the following above? Thanks!

  • Comment on Extracting single element from array to be printed in a one liner hash

Replies are listed 'Best First'.
Re: Extracting single element from array to be printed in a one liner hash
by toolic (Bishop) on May 20, 2014 at 13:55 UTC
Re: Extracting single element from array to be printed in a one liner hash
by Lotus1 (Vicar) on May 20, 2014 at 13:55 UTC
    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){
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1086785]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-19 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found