0

In this program I have function inside function Without sub function "output" the program is looping file and printing in the csv file.

But with sub function "output" it is not looping and it is writing only the first type to the csv file?

 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"); 


    }

}

1 Answers1

0

It appears that you're compiling your @data, but not actually outputting it. Perhaps this was just a bug during development?

Move the call to $csv->print to your loop:

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

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

        #exit if $ctr >2;
    }

Update

And since you asked for how to do it using a subroutine:

    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);
}
Miller
  • 34,962
  • 4
  • 39
  • 60
  • How can use inside the funstion? – user3488150 Apr 02 '14 at 07:12
  • You'd have to pass your `@data` values as parameters to the function. Then do your output to $csv. But truth is, that's a bad idea too, because you'd still be relying on $csv as a global. Don't do this. If this is the only place this functionality is used, no reason to encapsulate it out. – Miller Apr 02 '14 at 07:15
  • Now how can is pass @data as parameters can i give me a sample code for that? – user3488150 Apr 02 '14 at 07:20
  • sure, I've made an update on how to do it using a subroutine. – Miller Apr 02 '14 at 07:25
  • Yeah i understand but i dont want to call that "amazing_subroutine" inside the loop i want to process that function when user types 2 "Check with the uer input i have gave in my code."Print 2.TSV"" – user3488150 Apr 02 '14 at 07:31