http://www.perlmonks.org?node_id=1040918

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

Hey I have written a sub routine that aims to extract the column of csv file .
sub column_segregation { my $o=$_[0]; my $csv = Text::CSV->new ({ binary => 1 }); while (my $row = $csv->getline($io)) { push @array_A2, $row->[$o]; } return (@array_A2); }

So what actually is happening , is that for multiple function calls , the sending parameter is our column number . The $o is getting updated but the return via @array_A2 is always corresponding to the first call . So I figured might be the array_A2 is not getting overwritten so just after the sub routine name I added

@array_A2=();

now what is happening is that it always return an empty array

i want that the sub routine should return the array having entries corresponding to a particular column . I know I am posting this as a different question , because I really cannot figure out why is this happening that $o is getting updated but not in push @array_A2, $row->$o;

Replies are listed 'Best First'.
Re: issue with column extraction in perl
by poj (Abbot) on Jun 27, 2013 at 06:54 UTC
    You need to open the file handle for each column. You can ensure that by including the code in the sub like this ;
    #!perl use strict; use Text::CSV; my @ar; @ar = column_segregation('test.csv',0); print "@ar\n"; @ar = column_segregation('test.csv',3); print "@ar\n"; sub column_segregation { my ($file,$col) = @_; my @array_A2 = (); open my $io,'<',$file or die "$!"; my $csv = Text::CSV->new ({ binary => 1 }); while (my $row = $csv->getline($io)) { push @array_A2, $row->[$col]; } close $io; return (@array_A2); } __DATA__ 1,2,3,4,5 a,b,c,d,e 6,7,8,9,0 x,z,y,q.w
    poj
Re: issue with column extraction in perl
by kevbot (Vicar) on Jun 27, 2013 at 07:03 UTC

    It's hard for me to tell what your problem might be, since the code you posted looks incomplete. I highly recommend using the following at the top of your script.

    use strict; use warnings;

    The warning or errors you get will give you clues how to fix your code.

    Here is working code for printing out the first three columns of a test.csv file.

    #!/usr/bin/env perl use strict; use warnings; use Text::CSV; my @c1 = column_segregation(0); print join(", ", @c1), "\n"; my @c2 = column_segregation(1); print join(", ", @c2), "\n"; my @c3 = column_segregation(2); print join(", ", @c3), "\n"; exit; sub column_segregation { my $o = $_[0]; my @array_A2; my $csv = Text::CSV->new ({ binary => 1 }); open (my $io, "<", "test.csv") or die "Could not open test.csv"; while (my $row = $csv->getline($io)) { push @array_A2, $row->[$o]; } return (@array_A2); }
Re: issue with column extraction in perl
by rnewsham (Curate) on Jun 27, 2013 at 07:02 UTC

    You do not have $io in the sub you need to pass this to it or open the file within that sub.

    use strict; use warnings; use Text::CSV; open my $fh, "<:encoding(utf8)", "test.csv" or die "test.csv: $!"; my @rows = column_segregation( $fh, 2 ); print "$_\n" for @rows; close $fh; sub column_segregation { my ( $io, $o ) = @_; my $csv = Text::CSV->new ({ binary => 1 }); my @array_A2; while (my $row = $csv->getline($io)) { push @array_A2, $row->[$o]; } return (@array_A2); }
    Input 1,2,3,4 5,6,7,8 a,b,c,d Output 3 7 c