I have a file with almost 1,500 names of Marvel heroes, each name in new line. I have to ask user what his favourite hero is and find out if it's a hero from the list or not. Here's what I have right now. It doesn't work: I can guess only the last hero from the list. For the rest it just prints that they are not on the list.
print "Whats your favourite hero?\n";
my $hero = <stdin>;
chomp $hero;
open FILE, "<list_marvel.txt";
my @marvel = <FILE>;
chomp(@marvel);
my $result = 0;
foreach (@marvel) {
if ($_ eq $hero);
}
if ($result == 1) {
print "That hero is on the list";
}
else {
print "$hero is not on the list.\n";
}
Here are two files: -Perl code : Perl Code -List of heroes : List
openand check if you were successful. Like this:open FILE, '<', 'list_marvel.txt' or die "error on opening:$!'. And honestly, it seems to me like you're getting taught a perl that is over ten years old. If you want to use it somewhere else, I strongly suggest that you pick up some books like ”Learning perl” and “Modern Perl” to complement your classes.