Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Need help with Text::CSV

by kevbot (Vicar)
on Oct 28, 2015 at 05:42 UTC ( [id://1146226]=note: print w/replies, xml ) Need Help??


in reply to Need help with Text::CSV

Hi ravi45722,

Please post the code that you are actually running. If I run your script, I get this output:

Can't locate method setDelimiter at rb.pl line 8.
The documentation for Text::CSV states that this is how one defines the delimiter,
my $csv = Text::CSV->new({ sep_char => '|' });
So, here is a slightly modified version of your script.
#!/usr/bin/perl use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new({ sep_char => '|' }); # create a new object my $sample_input_string = '""|""|""|""|""|""|""|""|Submit|""|""|""|GSM +|GSM|'; if ($csv->parse($sample_input_string)) { my @field = $csv->fields; my $count = 0; for my $column (@field) { print ++$count, " => ", $column, "\n"; } print "\n"; } else { my $err = $csv->error_input; print "parse() failed on argument: ", $err, "\n"; }
The output is:
1 => 2 => 3 => 4 => 5 => 6 => 7 => 8 => 9 => Submit 10 => 11 => 12 => 13 => GSM 14 => GSM 15 =>
I'm not sure why you expect your output to be:
Submit|GSM|GSM => 560
as that does not seem consistent with your script or your input data.

Replies are listed 'Best First'.
Re^2: Need help with Text::CSV
by ravi45722 (Pilgrim) on Oct 28, 2015 at 05:52 UTC

    I posted my code as it is. And I got that  $csv->setDelimiter('|'); from http://search.cpan.org/~erangel/Text-CSV/CSV.pm. I posted my requirement above. Is it possible to read a file and group as per my conditions through this module??

      The link you posted is to a very old (2006) version of Text::CSV. It was also marked as an UNAUTHORIZED RELEASE. If you are using a more recent version of Text::CSV, the following should work to count the records that satisfy your criteria:
      #!/usr/bin/perl use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new({ sep_char => '|' }); open my $fh, "<", "ravi.csv" or die "ravi.csv: $!"; my $count = 0; while (my $row = $csv->getline($fh)){ my @fields = @{$row}; if($fields[9] eq 'Submit' and $fields[12] eq 'GSM' and $fields[13] + eq 'SMPP'){ ++$count; } } print "Count is: $count\n"; exit;

        Ya I understand my problem now. But I solved this problem earlier in a way.

        foreach my $Postpaid_file (@Postpaid_files) { open (POST ,"$Postpaid_file") or print "cannot open file\n +"; while(my $cdr_post_line = <POST>) { chomp $cdr_post_line; my @cdr_post_array = split(/\|/,$cdr_post_line); if ($cdr_post_array[12] eq "GSM" && $cdr_post_array[9] + eq "Submit") { $cdr_post_MO_cnt++ if any { /^\Q$cdr_post_arra +y[11]\E$/ } @MOresp_error; $Total_GSM_attempts++ unless any { /^\Q$cdr_po +st_array[11]\E$/ } @MOresp_error; } if ($cdr_post_array[12] eq "SMPP" && $cdr_post_array[9 +] eq "Submit" && $cdr_post_array[10] eq "0") { $cdr_post_AO_cnt++; #if any { /^\Q$cdr_post_ +array[11]\E$/ } @MOresp_error; $total_AO_success++ unless any { /^\Q$cdr_post +_array[11]\E$/ } @MOresp_error; $Total_SMPP_attempts++ unless any { /^\Q$cdr_p +ost_array[11]\E$/ } @MOresp_error; } if ($cdr_post_array[12] eq "GSM" && ($cdr_post_array[1 +3] eq "GSM" || $cdr_post_array[13] eq "ESMEGW") && $cdr_post_array[9] + eq "Submit") { $total_MT_P2P++ unless any { /^\Q$cdr_post_arr +ay[11]\E$/ } @MOresp_error; } if ($cdr_post_array[12] eq "GSM" && ($cdr_post_array[1 +3] eq "GSM" || $cdr_post_array[13] eq "ESMEGW") && $cdr_post_array[9] + eq "Submit" && $cdr_post_array[7] eq "Delivered") { $total_MT_P2P_success++; }

        I thought we can do a better code (by not using all if's to check the conditions) using modules. Almost here also we are doing same. Then what's the benefit using this module. Or is there any other way to simpler my code(above mentioned)????

        Update

        The main reason to simplify my code is its become 624 lines which contains mostly if's(nearly one if for every four lines) and to check "eq && eq && eq". Its bulky, error prone & (important one) I dont like this :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1146226]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-24 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found