나는 비교적 새로운 Perl이고 나는이 프로젝트를 통해 내가 힘든 시간을 보냈다. 프로젝트의 목적은 두 개의 CSV 파일을 비교하는 것입니다. 그 중 하나는 $ name, $ model, $ version 이고 다른 하나는 $ name2, $ disk, $ storage RESULT 파일에는 일치하는 행이 포함될 것이며 $ name, $ model, $ version, $ disk, $ storage와 같은 정보를 함께 넣을 것입니다.배열의 라인을 건너 뛰기, Perl
나는이 일을 처리했지만 내 문제는 프로그램을 빠뜨린 요소 중 하나가 고장 났을 때이다. 파일에서 요소가 누락 된 행을 발견하면 해당 행에서 중지합니다. 이 문제를 어떻게 해결할 수 있습니까? 어쩌면 내가 그 라인을 건너 뛰고 계속할 수있는 방법에 대한 제안이나 방법?
while (<FILE>) {
($name, $model, $version) = split(",");
next unless($name && $model && $version);
...;
}
당신은 무엇에 따라 사용 조건 : 당신은 당신이 다음을 즉시 현재 반복을 완료하고 시작할 수있는 next을 원한다고 생각
open(TESTING, '>testing.csv'); # Names will be printed to this during testing. only .net ending names should appear
open(MISSING, '>Missing.csv'); # Lines with missing name feilds will appear here.
#open (FILE,'C:\Users\hp-laptop\Desktop\file.txt');
#my (@array) =<FILE>;
my @hostname; #stores names
#close FILE;
#***** TESTING TO SEE IF ANY OF THE LISTED ITEMS BEGIN WITH A COMMA AND DO NOT HAVE A NAME.
#***** THESE OBJECTS ARE PLACED INTO THE MISSING ARRAY AND THEN PRINTED OUT IN A SEPERATE
#***** FILE.
#open (FILE,'C:\Users\hp-laptop\Desktop\file.txt');
#test
if (open(FILE, "file.txt")) {
}
else {
die " Cannot open file 1!\n:$!";
}
$count = 0;
$x = 0;
while (<FILE>) {
($name, $model, $version) = split(","); #parsing
#print $name;
chomp($name, $model, $version);
if (($name =~ /^\s*$/)
&& ($model =~ /^\s*$/)
&& ($version =~ /^\s*$/)) #if all of the fields are blank (just a blank space)
{
#do nothing at all
}
elsif ($name =~ /^\s*$/) { #if name is a blank
$name =~ s/^\s*/missing/g;
print MISSING "$name,$model,$version\n";
#$hostname[$count]=$name;
#$count++;
}
elsif ($model =~ /^\s*$/) { #if model is blank
$model =~ s/^\s*/missing/g;
print MISSING"$name,$model,$version\n";
}
elsif ($version =~ /^\s*$/) { #if version is blank
$version =~ s/^\s*/missing/g;
print MISSING "$name,$model,$version\n";
}
# Searches for .net to appear in field "$name" if match, it places it into hostname array.
if ($name =~ /.net/) {
$hostname[$count] = $name;
$count++;
}
#searches for a comma in the name feild, puts that into an array and prints the line into the missing file.
#probably won't have to use this, as I've found a better method to test all of the feilds ($name,$model,$version)
#and put those into the missing file. Hopefully it works.
#foreach $line (@array)
#{
#if($line =~ /^\,+/)
#{
#$line =~s/^\,*/missing,/g;
#$missing[$x]=$line;
#$x++;
#}
#}
}
close FILE;
for my $hostname (@hostname) {
print TESTING $hostname . "\n";
}
#for my $missing(@missing)
#{
# print MISSING $missing;
#}
if (open(FILE2, "file2.txt")) { #Run this if the open succeeds
#open outfile and print starting header
open(RESULT, '>resultfile.csv');
print RESULT ("name,Model,version,Disk, storage\n");
}
else {
die " Cannot open file 2!\n:$!";
}
$count = 0;
while ($hostname[$count] ne "") {
while (<FILE>) {
($name, $model, $version) = split(","); #parsing
#print $name,"\n";
if ($name eq $hostname[$count]) # I think this is the problem area.
{
print $name, "\n", $hostname[$count], "\n";
#print RESULT"$name,$model,$version,";
#open (FILE2,'C:\Users\hp-laptop\Desktop\file2.txt');
#test
if (open(FILE2, "file2.txt")) {
}
else {
die " Cannot open file 2!\n:$!";
}
while (<FILE2>) {
chomp;
($name2, $Dcount, $vname) = split(","); #parsing
if ($name eq $name2) {
chomp($version);
print RESULT"$name,$model,$version,$Dcount,$vname\n";
}
}
}
$count++;
}
#open (FILE,'C:\Users\hp-laptop\Desktop\file.txt');
#test
if (open(FILE, "file.txt")) {
}
else {
die " Cannot open file 1!\n:$!";
}
}
close FILE;
close RESULT;
close FILE2;
다음에 코드에 엄격하게 사용하십시오. 귀찮은 버그를 보호합니다. –
'use strict ','use warnings;', 코드를 올바르게 들여 쓰기, 어휘 파일 핸들과 함께 open의 인수 버전을 사용하고 배열 함수 ('push, map, grep') 사용법을 배우십시오. – dgw
당신이 펄을 가르치기 위해 사용하는 자료가 무엇이든지, 나는 그 형식을 삭제할 것을 강력히 권합니다 - 코드는 매우 오래된 것 (전역 이름 지정된 파일 핸들, 2-arg 형식의 열린 형식)에서 완전히 잘못된 것에 이르기까지 다양한 템플릿을 기반으로합니다. 개인적으로 받아들이지 마십시오. 분명히 잘못이 아닙니다.하지만 분명히 사용하고있는 것보다 훨씬 더 현대적이고 더 현대적인 책/학습서/코드 예제를 통해 매우 잘 배울 수 있습니다. – DVK