【问题标题】:perl looping and writing to csv?perl循环并写入csv?
【发布时间】:2014-04-02 07:05:49
【问题描述】:

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

但是使用子函数“输出”它不是循环的,它只将第一种类型写入 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"); 


    }

}

【问题讨论】:

    标签: perl loops csv foreach data-dumper


    【解决方案1】:

    您似乎正在编译您的@data,但实际上并未输出它。也许这只是开发过程中的一个错误?

    将对$csv-&gt;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);
    }
    

    【讨论】:

    • funstion里面怎么用?
    • 您必须将您的 @data 值作为参数传递给函数。然后将输出输出到 $csv。但事实是,这也是一个坏主意,因为您仍然依赖 $csv 作为全局变量。不要这样做。如果这是唯一使用此功能的地方,则没有理由将其封装出来。
    • 现在如何将@data 作为参数传递,我可以给我一个示例代码吗?
    • 当然,我已经更新了如何使用子例程进行操作。
    • 是的,我理解,但我不想在循环中调用“amazing_subroutine”,我想在用户键入 2 时处理该函数“检查我在代码中提供的 uer 输入。”打印 2 .TSV""
    猜你喜欢
    • 1970-01-01
    • 2016-09-30
    • 2018-01-19
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多