아래 스크립트는 텍스트 파일에서 함수 이름을 사용하고 여러 c, h 파일이 들어있는 폴더에서 검사합니다. 이 파일을 하나씩 열고 은 각 행을 읽습니다. 파일의 일부에서 일치하는 항목이 발견되면 줄 번호와 일치 항목이 포함 된 줄을 인쇄합니다.Perl의 다른 파일에서 한 파일의 문자열을 어떻게 찾을 수 있습니까?
비교가 제대로 작동하지 않는 것을 제외하면 모든 것이 잘 작동합니다. 나는 누구든지 내 문제를 해결해 주신 것에 매우 감사 할 것입니다.
#program starts:
use FileHandle;
print "ENTER THE PATH OF THE FILE THAT CONTAINS THE FUNCTIONS THAT YOU WANT TO
SEARCH: ";#getting the input file
our $input_path = <STDIN>;
$input_path =~ s/\s+$//;
open(FILE_R1,'<',"$input_path") || die "File open failed!";
print "ENTER THE PATH OF THE FUNCTION MODEL: ";#getting the folder path that
#contains multiple .c,.h files
our $model_path = <STDIN>;
$model_path =~ s/\s+$//;
our $last_dir = uc(substr ($model_path,rindex($model_path, "\\") +1));
our $output = $last_dir."_FUNC_file_names";
while(our $func_name_input = <FILE_R1>)#$func_name_input is the function name
#that is taken as the input
{
$func_name_input=reverse($func_name_input);
$func_name_input=substr($func_name_input,rindex($func_name_input,"\("+1);
$func_name_input=reverse($func_name_input);
$func_name_input=substr($func_name_input,index($func_name_input," ")+1);
#above 4 lines are func_name_input is choped and only part of the function
#name is taken.
opendir FUNC_MODEL,$model_path;
while (our $file = readdir(FUNC_MODEL))
{
next if($file !~ m/\.(c|h)/i);
find_func($file);
}
close(FUNC_MODEL);
}
sub find_func()
{
my $fh1 = FileHandle->new("$model_path//$file") or die "ERROR: $!";
while (!$fh1->eof())
{
my $func_name = $fh1->getline(); #getting the line
**if($func_name =~$func_name_input)**#problem here it does not take the
#match
{
next if($func_name=~m/^\s+/);
print "$.,$func_name\n";
}
}
}
$ func_name_input에 수행하는 reverse/substr/rindex 물건은 무엇입니까? – Schwern