繁体   English   中英

尝试解析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提出建议,请发布一个工作代码示例,因为我无法将其应用于在此说明的提议。 这就是我最终编写上述代码的原因。


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的行带来了“未初始化的值”相同的错误。

我也尝试了推@rows,$ row; 方法,但我真的不知道如何处理$ rows [$ i],因为它们是对数组(指针)的引用,并且我知道它们不能作为变量操作...找不到如何工作的有效示例使用它们。

您提供的错误消息以line 22, <$in> line 10.结尾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 diagnostics ,可以获取有关警告的更多详细信息(这是警告,而不是错误)。 您将获得如下内容:

(W未初始化)使用未定义的值,就好像它已经被定义一样。 它被解释为“”或0,但可能是一个错误。 要消除此警告,请为变量分配一个已定义的值。

为了帮助您弄清未定义的内容,perl会尝试告诉您未定义的变量的名称(如果有)。 在某些情况下,它不能执行此操作,因此它还会告诉您使用了未定义值的操作。但是,请注意,perl会优化您的程序,警告中显示的操作不一定会在您的程序中真实显示。 例如,通常将“ that $ foo”优化为“ that”。 $ foo,即使没有,警告也将引用串联(。)运算符。 在您的程序中。

因此,当您填充$word ,它没有值。 大概是因为输入文件中的某些行在那里有空记录。

我无法知道这是否对您的程序有效,因此我无法就如何解决此问题提供任何有用的建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM