john.tm has asked for the wisdom of the Perl Monks concerning the following question:
However i cannot get them to work if i combine them into a single script. The xlsx files are empty and the files are not deleted.
data#!/usr/bin/perl use strict; use warnings; use Spreadsheet::BasicRead; use Excel::Writer::XLSX; local $" = "'\n'"; my @classes; our $Header_row; #Set-up Files my $xlsx_WSD = ( "C:\\Temp\\Data\\data.xlsx"),, 1; my @csvtemp; if ( -e $xlsx_WSD ) { my $ss = new Spreadsheet::BasicRead($xlsx_WSD) or die; my $col = ''; my $row = 0; while ( my $data = $ss->getNextRow() ) { $row++; $col= join( "\t", @$data ); push @csvtemp, $col . "\n" if ( $col ne "" ); } } my @arraynew; my %seen; $Header_row = shift (@csvtemp); # this is where the headers a +re stored. foreach (@csvtemp){ chomp; $_ = uc $_ ; $_ =~ s/,//g; +#removes commas $_ =~ s/\t/,/g; #ch +ange from tab to csv push @arraynew, $_ . "\n" if !$seen{$_}++ ; #remove any + dupes } $Header_row =~ s/\t/,/g ; my (%seena, %clss_out); my @class_file; foreach (@arraynew) { chomp; my ($clss_col, $data) = $_ =~ m{^([A-z]*),(.*)}; next if $seen{$clss_col}{$data}++; append_data($clss_col, $_, \%clss_out); } csv_xlsx (); #------------------------------------------------------------------- sub append_data { my ($clss_col, $line, $clss_out) = @_; if (! $clss_out->{$clss_col}) { open($clss_out->{$clss_col}, '>', "C:\\Temp\\Data\\$clss_col.csv +") or die "Unable to open '$clss_col.csv' for appending: $!\n"; print { $clss_out->{$clss_col} } $Header_row; push @class_file, "C:\\Temp\\Data\\$clss_col.csv"; } print { $clss_out->{$clss_col} } $line . "\n"; } sub csv_xlsx{ my $dir = "C:\\Temp\\Data"; foreach my $fp (glob("$dir/*.csv")) { open my $fh, "<", $fp or die "can't read open '$fp'"; my $xlsx = $fp =~ s\csv\xlsx\; my $workbook = Excel::Writer::XLSX->new("$fp"); my $worksheet = $workbook->add_worksheet(); my ( $x, $y ) = ( 0, 0 ); while (<$fh>) { my @list = split /,/; foreach my $c (@list) { $worksheet->write( $x, $y++, $c ); } $x++; $y = 0; } $workbook->close(); close $fh or die "can't read close '$fp'"; } unlink glob "C:\\Temp\\Data\\*.csv" or warn $! ; }
CLASS,PUPIL,M/F,Year ZEBRAS,JAMES,M,3 LIONS,AMIE,M,1 GIRAFFES,AMES,M,R MEERKATS,JOHN,M,4 LEOPARDS,JIM,M,2 HIPPOS,PETER,M6 MONKEYS,PELE,M,5 ZEBRAS,DAN,M,3 MONKEYS,MINNIE,F,6 HIPPOS,REG,M,6
|
|---|