프로그램에서하는 일에 대해 혼란스러워 보였습니다.
$inFile = "animals.txt";
open (IN, $inFile) or die "Can't find file: $inFile";
@animallist = (<IN>);
# here you define a file name, open a file, and read all of the lines
# in the file into the array @animallist
foreach $line (@animallist) {
# here you iterate over all the lines, putting each line into $line
if ($line =~ $search) {
print "$line <br> <br>";
}
# here you perform the regex match: $line =~ /$search/ and if it
# succeeds, print $line
}
# here you end the loop
if ($line ne $search) {
print "$search isn't in the animal list";
}
# here you take the now uninitialized variable $line and try to match
# it against the as of yet undefined variable $search
# If both of these variables are undefined, and you are not using warnings
# it will simply return false (because "" ne "" is false)
# without warning about undefined variables in ne
당신은 당신의 전체 라인이 경우에도, 예를 들어, cat
, 당신은 여전히 파일에서 읽을 때, 그것은 뒤에 줄 바꿈을 가지고 있기 때문에, 문자열 cat
에 ne
를 사용하여 비교할 수 없다는 것을 알고 있어야합니다, 그래서 그것은 정말로 cat\n
입니다. 너 chomp
이 아니라면.
은으로 보이지만 파일을 읽은 후 파일에 $search
이 포함되어 있는지 확인할 수는 없습니다. 파일을 읽는 동안 그렇게해야합니다.
출처
2014-11-17 23:25:15
TLP
당신은 왜 그것이 작동하지 않는다고 생각합니까? – TLP
항상 맨 위를 넣으십시오 :'use strict; ' –
첫 번째 if 문은 작동하지만 파일에서 단어를 찾으면 마지막 print 문도 출력합니다. "$ 검색은 동물 목록에 없습니다" – TheHadimo