在数组中找不到字符串

时间:2015-06-10 07:57:05

标签: perl

我有一个文件,其中包含近1500名漫威英雄的名字,每个名字都在新行中。我必须询问用户他最喜欢的英雄是什么,并找出它是否是列表中的英雄。这就是我现在所拥有的。它不起作用:我只能猜测列表中的最后一个英雄。其余的只是打印它们不在列表中。

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";
}

这是两个文件: -Perl代码:Perl Code - 英雄列表:List

6 个答案:

答案 0 :(得分:1)

您忘了增加$result。如果您正确缩进代码,则更容易看到。

foreach (@marvel) {
    # here something is missing
    if ( $_ eq $hero );
}

在foreach中添加$result++ if $_ eq $hero;

您应始终use strictuse warnings。那可能会告诉你附近的语法错误;

还要考虑使用lexical文件句柄打开三个参数。

重写它看起来像这样:

use strict;
use warnings;
use feature 'say'; # gives you say, which is print with a newline at the end

say "What's you favourite hero?";
my $hero = <STDIN>;
chomp $hero;

# alsways name variables so it's clear what they are for
my $found = 0;

# die with the reason of error if something goes wrong
open my $fh, '<', 'list_marvel.txt' or die $!;

# read the file line by line    
while ( my $line = <$fh> ) {
  chomp $line;
  if ( $line eq $hero ) {
    # increment so we know we 'found' the hero in the list
    $found++;

    # stop reading at the first hit
    last;
  }
}
close $fh;

# no need to check for 1, truth is enough
if ( $result ) {
    say "That hero is on the list.";
}
else {
    say "$hero is not on the list.";
}

答案 1 :(得分:1)

您不能将$result置于任何地方。

像这样制作foreach循环:

foreach(@marvel){
  $result = $_ eq $hero;
}

foreach (@marvel){
  $result = 1 if $_ eq $hero
}

答案 2 :(得分:1)

您的程序出现语法错误,无法编译。它当然不会只找到名单上的姓氏

主要问题是您从未设置$result,而if($_ eq $hero)应该是$result = 1 if($_ eq $hero)

必须始终 use strictuse warnings位于您编写的每个Perl程序的顶部。找到直截了当的问题是一个巨大的帮助

这是一个有效的版本

use strict;
use warnings;

my $filename = 'list_marvel.txt';

open my $fh, '<', $filename or die qq{Unable to open "'list_marvel.txt'": $!};

print "Whats your favourite hero? ";
my $hero = <>;
chomp $hero;

my $found;
while ( <$fh> ) {
    chomp;
    if ( $_ eq $hero ) {
        ++$found;
        last;
    }
}

print $found ? "$hero is on the list\n" : "$hero is not on the list";

答案 3 :(得分:0)

首先,您错过了在$result附近设置if($_ eq $hero)

然后,您可能希望让您比较不区分大小写。这需要一个正则表达式,例如:

$result = 1 if (/^$hero$/i);

答案 4 :(得分:0)

刚刚修改了你的代码。如果条件增加$result之后。始终使用use strictuse warnings并始终使用3个参数来打开文件。

use strict;
use warnings;

print "Whats your favourite hero?\n";
my $hero = <stdin>;
chomp $hero;
open FILE, "<", "list_marvel.txt" or die $!;
chomp (my @marvel = <FILE>);
close FILE;

my $result = 0;

foreach my $name (@marvel)
{
    if($name eq $hero)
    {
        $result++;
    }
}
if ($result == 1)
{
    print "That hero is in the list.\n";
} 
else
{
    print "$hero is not in the list.\n";
}

答案 5 :(得分:0)

这将从STDIN获取单个用户条目。它将运行英雄名称文件,如果匹配用户条目,它将打印名称并退出循环。如果找不到该名称,它会告诉您:

use warnings;
use strict; 

open my $file1, '<', 'input.txt' or die $!;

print "Enter hero: ";
chomp(my $hero = <STDIN>);

my $result = 0;
while(<$file1>){
    chomp;
    if (/$hero/){   
        print "$_\n";
        $result++;
        last;
    }
}
print "hero not in list\n" if $result == 0;