Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Text::CSV_XS; Type Method

by drodinthe559 (Monk)
on Feb 18, 2010 at 16:57 UTC ( #823977=perlquestion: print w/replies, xml ) Need Help??

drodinthe559 has asked for the wisdom of the Perl Monks concerning the following question:

Guru's, I'm having difficulty forcing the integers for all my fields that I'm out putting. I defined the type but I still have fields out putting as character fields.
#!/usr/bin/perl -w use strict; use warnings; use Text::CSV_XS; my ($reporttype,$sorter2,$price,$account,$amount,$check_no,$date_time, +$prod_no,$rundate); $reporttype = 0; $sorter2 = 0; $account = 0; $amount = 0; $check_no = 0; my $procdate = sprintf "%02d%02d%02d%02d%02d%02d", (localtime)[4]+1, ( +localtime)[3], substr((localtime)[5]+1900,2,2),(localtime)[2], (local +time)[1], (localtime)[0]; open SOURCE, "< C:/B01.PRN" or die "can't open file $!"; open CSVFILE, ">> C:/test_" . $procdate . ".csv"; my $csv = Text::CSV_XS->new({eol => "\n"}); $csv->types ([Text::CSV_XS::IV (), Text::CSV_XS::IV (), Text::CSV_XS::IV (), Text::CSV_XS::IV ()]); while(my $source = <SOURCE>) { if (substr($source,7,4) =~ /\d\d\d\d/ && $reporttype == 1 && $sort +er2 == 1){ $prod_no = substr($source,88,8); $account = int(substr($source,22,10)); $price = substr($source,7,9); $check_no = substr($source,48,16); $amount = substr($source,32,16); #$amount =~ s/,//; #$amount = $amount; $csv->combine($prod_no, $account, $price, $check_no,$amount); print $csv->string; printf CSVFILE $csv->string; }; }; close SOURCE;

Replies are listed 'Best First'.
Re: Text::CSV_XS; Type Method
by Anonymous Monk on Feb 18, 2010 at 17:06 UTC
    my $procdate = sprintf "%02d%02d%02d%02d%02d%02d", (localtime)[4]+1, ( +localtime)[3], substr((localtime)[5]+1900,2,2),(localtime)[2], (local +time)[1], (localtime)[0];
    use POSIX; my $procdate = POSIX::strftime('%Y%m%d%H%M%S', localtime);
Re: Text::CSV_XS; Type Method
by ikegami (Pope) on Feb 18, 2010 at 17:03 UTC
    Please provide the data that's giving you a problem
      Here's an example:
      11111111,2222222222,5555-6666," 1234"," 123.45" 11111111,3333333333,7777-8888," 5678"," 567.89" 11111111,4444444444,1111-9999," 9123"," 176.47"
      Notice the last two columns have quotes. I'm trying to get rid of that.

        Trim the spaces:

        s/^\s+//, s/\s+\z// for $check_no, $amount;

        By the way, types is only used when parsing, so it does absolutely nothing in your program.

Re: Text::CSV_XS; Type Method
by snopal (Pilgrim) on Feb 18, 2010 at 17:11 UTC

    substr is certainly a text context. Perhaps that is a constraint that is hard for combine to break.

    I'd try something like:

    $prod_no = 0 + substr($source,88,8);

    And check those results.

      That works as long as the field has data. I get the following error:
      Argument " " isn't numeric in abs at Y:\Perl Projects\C +oopers\ line 57, <SOURCE> line 12089.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://823977]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2020-12-04 23:59 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (63 votes). Check out past polls.