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!


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.