Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Extracting single element from array to be printed in a one liner hashby 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!
Back to
Seekers of Perl Wisdom
|
|