gpt4 book ai didi

arrays - 跳过数组中的一行,Perl

转载 作者:行者123 更新时间:2023-12-02 09:25:37 24 4
gpt4 key购买 nike

我对 Perl 比较陌生,我遇到过这个项目,但我遇到了一些困难。该项目的目标是比较两个 csv 文件,其中之一包含:$名称、$型号、$版本另一个包含:$name2,$磁盘,$存储最后,RESULT 文件将包含匹配的行并将信息组合在一起,如下所示:$名称、$模型、$版本、$磁盘、$存储。

我已经成功做到了这一点,但我的问题是,当缺少其中一个元素时,程序就会中断。当它在文件中遇到缺少元素的行时,它会在该行停止。我该如何解决这个问题?有什么建议或方法可以让它跳过该行并继续吗?

这是我的代码:

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;

最佳答案

我想你想要next ,它可以让您立即完成当前迭代并开始下一个迭代:

while (<FILE>) {
( $name, $model, $version ) = split(",");
next unless( $name && $model && $version );
...;
}

您使用的条件取决于您接受的值。在我的示例中,我假设所有值都需要为 true。如果它们不需要是空字符串,也许您可​​以检查长度:

while (<FILE>) {
( $name, $model, $version ) = split(",");
next unless( length($name) && length($model) && length($version) );
...;
}

如果您知道如何验证每个字段,则可能有这些字段的子例程:

while (<FILE>) {
( $name, $model, $version ) = split(",");
next unless( length($name) && is_valid_model($model) && length($version) );
...;
}

sub is_valid_model { ... }

现在您只需要决定如何将其整合到您已经在做的事情中。

关于arrays - 跳过数组中的一行,Perl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11073557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com