Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
perl循环和写入csv?_Perl_Loops_Csv_Foreach_Data Dumper - Fatal编程技术网

perl循环和写入csv?

perl循环和写入csv?,perl,loops,csv,foreach,data-dumper,Perl,Loops,Csv,Foreach,Data Dumper,在这个程序中,我有函数内部的函数,没有子函数“输出”。程序正在循环文件并在csv文件中打印 但使用子函数“output”时,它不会循环,只会将第一种类型写入csv文件 sub outputType(){ print "Choose Your Output Method"."\n"; print "1.Dumper". "\n"; print "2.TSV". "\n"; print $input; chomp($cinput = <>);

在这个程序中,我有函数内部的函数,没有子函数“输出”。程序正在循环文件并在csv文件中打印

但使用子函数“output”时,它不会循环,只会将第一种类型写入csv文件

 sub outputType(){
    print "Choose Your Output Method"."\n";
    print "1.Dumper". "\n";
    print "2.TSV". "\n";
    print $input;
    chomp($cinput = <>);
    nswitch($cinput){
       case 1 : {csv();} #Function CSV will be called based on the user request 
       case 2 : {output();} #Function Output will be called based on user request
       default : {print "Enter Correct value". "\n"; if($cinput>3 || $cinput ne "\d+"){exit;}}
}

}
outputType();

sub csv(){

    my $csv = Text::CSV_XS->new ({ binary => 1 }) or
    die "Cannot use CSV: ".Text::CSV_XS->error_diag ();
    #$csv->eol ("\r\n");
    open my $f1, "<:encoding(utf8)", "upcs.tsv" or die "upcs.tsv: $!";
    #open my $f1, '<', 'upcs.tsv';

    my $sem3 = Net::Semantics3::Products->new (
        api_key => 'SEM3F9C9',
        api_secret => 'OGU4N2M3OTI4NGY',
    );

    #my $csv = Text::CSV->new ({
         #'quote_char'  => '',
         #'escape_char' => '',
     #   'sep_char'    => "\t",
      #   'binary'      => 1
     #});

    my @field;
    while(my $rows = $csv->getline ($f1)){
    #print STDERR Dumper $rows->[0];
    my $a = $rows->[0]."\n";
    push(@field,$a);
    #$csv -> print($f3,$_) for \@field
    }
    #close $f1;
    #$csv->eol ("\r\n");


    #print scalar(@field);


    open my $f3, ">:encoding(utf8)", "frameworkresult.csv" or die "frameworkresult.csv: $!";
    $csv->eol ("\r\n"); 

    #open my $f3, '>', 'frameworkresult.tsv';
    my @headers = qw/
    Name
    UPC
    Price/;
    $csv->print ($f3, $_) for \@headers;
    $csv->eol ("\r\n"); 
    my $ctr= 0;


    foreach my $input (@field){

        print $input;
        $sem3->add("products","upc",$input);
        my $nextProductsRef = $sem3->get_products();

        my $results = $nextProductsRef->{results};
        my @data;
        foreach my $result (@$results) {
            $ctr++;

            #print STDERR " Name: ".$result->{name}."\n";

            #print STDERR " UPC: ".$result->{upc}."\n";

            #print STDERR " Price: ".$result->{price}."\n";
            my $f = $result->{name};
            my $g = $result->{upc};
            my $h = $result->{price};
            push(@data,$f);
            push(@data,$g);
            push(@data,$h);

            #exit if $ctr >2;
        }
        print Dumper(@data);
        sub output(){
           $csv->print($f3,\@data);
        }
        $csv->eol ("\r\n"); 


    }

}
子输出类型(){
打印“选择输出方法”。\n;
打印“1.转储程序”。\n;
打印“2.TSV”。\n;
打印$input;
chomp($cinput=);
nswitch($cinput){
案例1:{csv();}#函数csv将根据用户请求调用
案例2:{output();}#将根据用户请求调用函数输出
默认值:{print“输入正确的值”。“\n”;如果($cinput>3 | |$cinput ne”\d+”{exit;}
}
}
outputType();
子csv(){
我的$csv=Text::csv_XS->new({binary=>1})或
die“无法使用CSV:”.Text::CSV_XS->错误诊断();
#$csv->eol(“\r\n”);
打开我的$f1、“:encoding(utf8)”、“frameworkresult.csv”或“frameworkresult.csv:$!”;
$csv->eol(“\r\n”);
#打开我的$f3,“>”,“frameworkresult.tsv”;
我的@headers=qw/
名称
UPC
价格/;
\@标题的$csv->打印($f3,$\);
$csv->eol(“\r\n”);
我的$ctr=0;
foreach my$输入(@field){
打印$input;
$sem3->添加(“产品”、“upc”和$input);
my$nextProductsRef=$sem3->get_products();
my$results=$nextProductsRef->{results};
我的@数据;
foreach my$result(@$results){
$ctr++;
#打印STDERR“Name:”.$result->{Name}.\n;
#打印STDERR“UPC:.$result->{UPC}.\n”;
#打印STDERR“Price:.$result->{Price}.\n”;
my$f=$result->{name};
my$g=$result->{upc};
my$h=$result->{price};
推送(@data,$f);
推送(@data,$g);
推送(@data,$h);
#如果$ctr>2,则退出;
}
打印转储程序(@data);
子输出(){
$csv->打印($f3,\@数据);
}
$csv->eol(“\r\n”);
}
}

看起来您正在编译
@数据,但实际上并没有输出它。也许这只是开发过程中的一个bug

将对
$csv->print
的调用移动到循环中:

        push(@data,$f);
        push(@data,$g);
        push(@data,$h);

        $csv->print($f3,\@data);

        #exit if $ctr >2;
    }
更新

既然你问了如何使用一个子程序:

    push(@data,$f);
    push(@data,$g);
    push(@data,$h);

    amazing_subroutine($csv, $f3, \@data);

    #exit if $ctr >2;
}

sub amazing_subroutine {
    my ($csv, $f3, $dataref) = @_;
    $csv->print($f3, $dataref);
}

如何在函数内部使用?必须将
@data
值作为参数传递给函数。然后输出到$csv。但事实是,这也是一个坏主意,因为您仍然依赖于$csv作为全局应用程序。不要这样做。如果这是唯一使用此功能的地方,没有理由封装它。现在如何将@data作为参数传递?我可以给我一个示例代码吗?当然,我已经更新了如何使用子例程。是的,我理解,但我不想称之为“惊人的子例程”在循环中,当用户键入2“检查我在代码中提供的uer输入”时,我希望处理该函数。“打印2.TSV”