尝试解析 csv 文件时声明错误
Errors in declaration when trying to parse a csv file
我正在尝试解析格式如下的 CSV 文件:
dog cats,yellow blue tomorrow,12445
birds,window bank door,-novalue-
birds,window door,5553
aspirin man,red,567
(写-novalue-的地方没有值)
use strict;
use warnings;
my $filename = 'in.txt';
my $filename2 = 'out.txt';
open(my $in, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";
my $word = "";
while (my $row = <$in>) {
chomp $row;
my @fields = split(/,/,$row);
#Save the first word of the second column
($word) = split(/\s/,$fields[1]);
if ($word eq 'importartWord')
{
printf $out "$fields[0]".';'."$word".';'."$fields[2]";
}
else #keep as it was
{
printf $out "$fields[0]".';'."$fields[1]".';'."$fields[2]";
}
Use of uninitialized value $word in string ne at prueba7.pl line 22, <$in> line 10.
无论我在何处定义 $word,我都无法停止收到该错误并且无法理解原因。我想我已经正确地初始化了 $word。非常感谢您的帮助。
如果你要建议使用 Text::CSV post 一个工作代码示例,因为我无法将它应用到我在此处解释的建议中。这就是我最终编写上述代码的原因。
PD:
因为我知道你会使用 Text::CSV 来询问我以前的代码,所以这里是:
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new({ sep_char => ';', binary => 1 }) or
die "Cannot use CSV: ".Text::CSV->error_diag ();
#directorio donde esta esc_prim2.csv
my $file = 'C:\Users\Sergio\Desktop\GIS\perl\esc_prim2.csv';
my $sal = 'C:\Users\Sergio\Desktop\GIS\perl\esc_prim3.csv';
open my $data, "<:encoding(utf8)", "$file" or die "$file: $!";
open my $out, ">:encoding(utf8)", "$sal" or die "$sal: $!";
$csv->eol ("\r\n");
#initializing variables
my $row = "";
my $word = "";
my $validar = 0;
my $line1 = "";
my @mwords = [""];#Just a try to initialize mwords... doesn't work, error keeps showing
#save the first line with field names on the other file
$line1 = <$data>;
$csv->parse($line1);
my @fields = $csv->fields();
$csv->print($out,[$fields[0], $fields[1], $fields[2]]);
while ($row = <$data>) {
if ($csv->parse($row)) {
@fields = $csv->fields();
#save first word of the field's second element
@mwords = split (/\s/, $fields[1]);
#keep the first one
$word = $mwords[0];
printf($mwords[0]);
#if that word is not one of SAN, EL y LA... writes a line in the new file with the updated second field.
$validar = ($word ne 'SAN') && ($word ne 'EL') && ($word ne 'LA');
if ($validar)
{
$csv->print($out,[$fields[0], $word, $fields[2]]);
}
else { #Saves the line in the new file as it was in the old one.
$csv->print($out,[$fields[0], $fields[1], $fields[2]]);
}
} else {#error procesing row
warn "La row no se ha podido procesar\n";
}
}
close $data or die "$file: $!";
close $out or die "$sal: $!";
这里声明 $validar 的行带来了与 "uninitialized value" 相同的错误,尽管我这样做了。
我也尝试了 push @rows, $row; 方法,但我真的不知道如何处理 $rows[$i] 因为它们是对数组的引用(指针)并且我知道它们不能作为变量操作...找不到关于如何使用它们的工作示例。
您提供的错误消息以以下结尾:line 22, <$in> line 10.
但您的问题没有显示数据的第 10 行 ($in
) 需要对此答案进行一些推测 - 但是,我会说in.txt
第 10 行的第二个字段 $field[1]
为空。
因此,这一行:($word) = split(/\s/,$fields[1]);
导致 $word
未定义。因此,后面的一些使用 - 无论是 ne
运算符(如消息中所示)还是其他任何东西都会产生错误。
顺便说一句——在字符串中单独插入一个变量没有什么意义;而不是 "$fields[0]"
,说 $fields[0]
除非你要在那里放一些东西,比如 "$fields[0];"
。您可能需要考虑更换
printf $out "$fields[0]".';'."$word".';'."$fields[2]";
与
printf $out $fields[0] . ';' . $word . ';' . $fields[2];
或
printf $out "$fields[0];$word;$fields[2]";
当然,TMTOWTDI - 所以你可能想告诉我管好我自己的事。 :-)
我认为您误解了错误。这不是变量声明的问题,而是您放入变量中的数据的问题。
Use of uninitialized value
这意味着您正在尝试使用未定义(不是未声明)的值。这意味着您正在使用一个您还没有赋值的变量。
您可以通过将 use diagnostics
添加到您的代码中来获取有关警告(这是警告,而不是错误)的更多详细信息。你会得到这样的东西:
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl will try to tell you
the name of the variable (if any) that was undefined. In some cases
it cannot do this, so it also tells you what operation you used the
undefined value in. Note, however, that perl optimizes your program
and the operation displayed in the warning may not necessarily appear
literally in your program. For example, "that $foo" is usually
optimized into "that " . $foo, and the warning will refer to the
concatenation (.) operator, even though there is no . in
your program.
因此,当您填充 $word
时,它没有获得值。据推测,那是因为您的输入文件中的某些行在那里有一个空记录。
我无法知道这是否是您程序的有效输入,因此我无法就如何解决此问题提供任何有用的建议。
我正在尝试解析格式如下的 CSV 文件:
dog cats,yellow blue tomorrow,12445
birds,window bank door,-novalue-
birds,window door,5553
aspirin man,red,567
(写-novalue-的地方没有值)
use strict;
use warnings;
my $filename = 'in.txt';
my $filename2 = 'out.txt';
open(my $in, '<:encoding(UTF-8)', $filename)
or die "Could not open file '$filename' $!";
my $word = "";
while (my $row = <$in>) {
chomp $row;
my @fields = split(/,/,$row);
#Save the first word of the second column
($word) = split(/\s/,$fields[1]);
if ($word eq 'importartWord')
{
printf $out "$fields[0]".';'."$word".';'."$fields[2]";
}
else #keep as it was
{
printf $out "$fields[0]".';'."$fields[1]".';'."$fields[2]";
}
Use of uninitialized value $word in string ne at prueba7.pl line 22, <$in> line 10.
无论我在何处定义 $word,我都无法停止收到该错误并且无法理解原因。我想我已经正确地初始化了 $word。非常感谢您的帮助。
如果你要建议使用 Text::CSV post 一个工作代码示例,因为我无法将它应用到我在此处解释的建议中。这就是我最终编写上述代码的原因。
PD: 因为我知道你会使用 Text::CSV 来询问我以前的代码,所以这里是:
#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new({ sep_char => ';', binary => 1 }) or
die "Cannot use CSV: ".Text::CSV->error_diag ();
#directorio donde esta esc_prim2.csv
my $file = 'C:\Users\Sergio\Desktop\GIS\perl\esc_prim2.csv';
my $sal = 'C:\Users\Sergio\Desktop\GIS\perl\esc_prim3.csv';
open my $data, "<:encoding(utf8)", "$file" or die "$file: $!";
open my $out, ">:encoding(utf8)", "$sal" or die "$sal: $!";
$csv->eol ("\r\n");
#initializing variables
my $row = "";
my $word = "";
my $validar = 0;
my $line1 = "";
my @mwords = [""];#Just a try to initialize mwords... doesn't work, error keeps showing
#save the first line with field names on the other file
$line1 = <$data>;
$csv->parse($line1);
my @fields = $csv->fields();
$csv->print($out,[$fields[0], $fields[1], $fields[2]]);
while ($row = <$data>) {
if ($csv->parse($row)) {
@fields = $csv->fields();
#save first word of the field's second element
@mwords = split (/\s/, $fields[1]);
#keep the first one
$word = $mwords[0];
printf($mwords[0]);
#if that word is not one of SAN, EL y LA... writes a line in the new file with the updated second field.
$validar = ($word ne 'SAN') && ($word ne 'EL') && ($word ne 'LA');
if ($validar)
{
$csv->print($out,[$fields[0], $word, $fields[2]]);
}
else { #Saves the line in the new file as it was in the old one.
$csv->print($out,[$fields[0], $fields[1], $fields[2]]);
}
} else {#error procesing row
warn "La row no se ha podido procesar\n";
}
}
close $data or die "$file: $!";
close $out or die "$sal: $!";
这里声明 $validar 的行带来了与 "uninitialized value" 相同的错误,尽管我这样做了。
我也尝试了 push @rows, $row; 方法,但我真的不知道如何处理 $rows[$i] 因为它们是对数组的引用(指针)并且我知道它们不能作为变量操作...找不到关于如何使用它们的工作示例。
您提供的错误消息以以下结尾:line 22, <$in> line 10.
但您的问题没有显示数据的第 10 行 ($in
) 需要对此答案进行一些推测 - 但是,我会说in.txt
第 10 行的第二个字段 $field[1]
为空。
因此,这一行:($word) = split(/\s/,$fields[1]);
导致 $word
未定义。因此,后面的一些使用 - 无论是 ne
运算符(如消息中所示)还是其他任何东西都会产生错误。
顺便说一句——在字符串中单独插入一个变量没有什么意义;而不是 "$fields[0]"
,说 $fields[0]
除非你要在那里放一些东西,比如 "$fields[0];"
。您可能需要考虑更换
printf $out "$fields[0]".';'."$word".';'."$fields[2]";
与
printf $out $fields[0] . ';' . $word . ';' . $fields[2];
或
printf $out "$fields[0];$word;$fields[2]";
当然,TMTOWTDI - 所以你可能想告诉我管好我自己的事。 :-)
我认为您误解了错误。这不是变量声明的问题,而是您放入变量中的数据的问题。
Use of uninitialized value
这意味着您正在尝试使用未定义(不是未声明)的值。这意味着您正在使用一个您还没有赋值的变量。
您可以通过将 use diagnostics
添加到您的代码中来获取有关警告(这是警告,而不是错误)的更多详细信息。你会得到这样的东西:
(W uninitialized) An undefined value was used as if it were already defined. It was interpreted as a "" or a 0, but maybe it was a mistake. To suppress this warning assign a defined value to your variables.
To help you figure out what was undefined, perl will try to tell you the name of the variable (if any) that was undefined. In some cases it cannot do this, so it also tells you what operation you used the undefined value in. Note, however, that perl optimizes your program and the operation displayed in the warning may not necessarily appear literally in your program. For example, "that $foo" is usually optimized into "that " . $foo, and the warning will refer to the concatenation (.) operator, even though there is no . in your program.
因此,当您填充 $word
时,它没有获得值。据推测,那是因为您的输入文件中的某些行在那里有一个空记录。
我无法知道这是否是您程序的有效输入,因此我无法就如何解决此问题提供任何有用的建议。