in reply to Spreadsheet::ParseExcel assigning the column

You just need to use Text::CSV to output the new data to a csv file:
#!/software/bin/perl BEGIN { unshift @INC, '/nfs/users/nfs_d/dmb/perl/lib' ; unshift @INC, '/nfs/users/nfs_d/dmb/perl/lib2' ; } use File::Basename ; use Algorithm::Permute ; use Spreadsheet::ParseExcel; use Getopt::Long; use Text::CSV; use strict; use warnings ; my @required_col = (0,2,13..47); GetOptions( "if=s" => \my $excel_file, "od=s" => \my $outputdir, ); my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->Parse($excel_file); if ( !defined $workbook ) { die $parser->error(), ".\n"; } my $newfile = $excel_file . ".csv"; my $csv = Text::CSV->new ( { eol => "\n" } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, '>', $newfile or die "Can't open $newfile: $!"; my @worksheets = $workbook->worksheets(); warn "More than 1 worksheet found\n" if @worksheets > 1; # Only work with first page. my $worksheet = $worksheets[0]; my ( $row_min, $row_max ) = $worksheet->row_range(); # Skip header row for my $row ( 1 .. $row_max ) { my @data = map { my $cell = $worksheet->get_cell($row, $_); $cell ? $cell->value() : ''; } @required_col; print "@data\n"; $csv->print($fh, \@data); }

Replies are listed 'Best First'.
Re^2: Spreadsheet::ParseExcel assigning the column
by Anonymous Monk on Mar 29, 2011 at 15:13 UTC
    Hi thanks,! Is there any way to return the headers(or the column descriptors) of the Excel file using Spreadsheet::ParseExcel? At the moment am getting them by,
    for my $row ( 0 .. 0 ) { @value = map { my $cell = $worksheet->get_cell($row, $_); $cell ? $cell->value() : ''; } @required_col;
    But, I couldn't pass the @value array to another subroutine; which takes an array of data as mentioned before.

    I need both the arrays in the next subroutine.
    #!/software/bin/perl BEGIN { unshift @INC, '/nfs/users/nfs_d/dmb/perl/lib' ; unshift @INC, '/nfs/users/nfs_d/dmb/perl/lib2' ; unshift @INC, '/nfs/users/nfs_a/aj6/CGP/perl_stuffs/lib/lib/site_p +erl/5.8.8/'; } use File::Basename ; use Algorithm::Permute ; use Spreadsheet::ParseExcel; use Getopt::Long; use Text::CSV; use strict; use warnings ; my @required_col = (0,1,2,13..47); GetOptions( "if=s" => \my $excel_file, "od=s" => \my $outputdir, ); my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->Parse($excel_file); if ( !defined $workbook ) { die $parser->error(), ".\n"; } my $newfile = $excel_file . ".csv"; my $csv = Text::CSV->new ( { eol => "\n" } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, '>', $newfile or die "Can't open $newfile: $!"; my @worksheets = $workbook->worksheets(); warn "More than 1 worksheet found\n" if @worksheets > 1; # Only work with first page. my $worksheet = $worksheets[0]; my @hea = $worksheet->{Header}; print @hea; my ( $row_min, $row_max ) = $worksheet->row_range(); my @value; ##to get the headers of the excel file; for my $row ( 0 .. 0 ) { @value = map { my $cell = $worksheet->get_cell($row, $_); $cell ? $cell->value() : ''; } @required_col; } # Skip header row my @data; for my $row ( 1 .. $row_max ) { @data = map { my $cell = $worksheet->get_cell($row, $_); $cell ? $cell->value() : ''; } @required_col; &changeformat(\@value,\@data); } sub changeformat{ my ($header,$data) = @_; my(%hash1,%hash2); my $plex1 = shift @$header; #since passed through the loop again and +again, shifts the first element every time it passes through the loop $hash2{$plex1} = \@$header; my $plex = shift @$data; my $sangerid = shift @$data; my $supplierid = shift @$data; $hash1{$supplierid} = \@$data; foreach my $key (keys %hash1){ foreach my $k(keys %hash2){ for(my$i = 0;$i<=scalar(@$data);$i++){ # print "$key,$hash1{$key}[$i], $plex,$hash2{$k}[$i]\n" if ($hash +2{$k}[$i] and $key); } } } }

    Any fix /suggestions how to pass it?

    thanks a ton

      This

      my @value; ##to get the headers of the excel file; for my $row ( 0 .. 0 ) { @value = map { my $cell = $worksheet->get_cell($row, $_); $cell ? $cell->value() : ''; } @required_col; }

      can be simplified to just

      ##to get the headers of the excel file; my @value = map { my $cell = $worksheet->get_cell(0, $_); $cell ? $cell->value() : ''; } @required_col;

      And since you're modifying the arrays passed to your subroutine, it looks like you just need to dereference them so that you make a copy.

      sub changeformat { my ($header,$data) = @_; my @header = @$header; my @data = @$data;

      Then only perform your operations on the new @header and @data arrays and not on $header or $data.