wildguess123 has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| 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 | |
by wildguess123 (Initiate) on May 20, 2014 at 14:02 UTC | |
by moritz (Cardinal) on May 20, 2014 at 14:07 UTC | |
by Lotus1 (Vicar) on May 20, 2014 at 17:47 UTC |