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

Hi all, I am using SpreadSheet::WriteExcel, and was wonduring if there is a function under that module to do loop. the problem I am runnig through is: my worksheet one contain a lot of datas like in sheet1 I have the following :
FileName ErrorType one.cxx 588 one.cxx 588 one.cxx 588 two.cxx 333 three.cxx 588 four.cxx 666 four.cxx 666 five.cxx 588 six.cxx 666
and I want in my sheet2 to have a summary like that
fileName Error 588 333 666 one.cxx 3 two.cxx 1 three.cxx 1 four.cxx 2 . . .
so basically , I am scaning the second column in sheet1 and counting the occurence of each error and display how many times it occures for each file in sheet2. Is there a trick to do the loop using the module , if not can someone give me a hint in writing that excel formula :(. Thanks

Moved to SoPW and added code tags - dvergin 2002-06-22

Replies are listed 'Best First'.
Re: WriteExcel
by particle (Vicar) on Jun 22, 2002 at 21:33 UTC
Re: WriteExcel
by RMGir (Prior) on Jun 23, 2002 at 02:20 UTC
    Keep in mind I'm whipping this up on a PC that doesn't even have perl installed, never mind SS:WE. But I think this'll work.

    If it looks like the code I posted for you yesterday, that's what I started from, since I don't have SS:WE's perldoc handy here.

    #!/usr/bin/perl -w use strict; use Spreadsheet::WriteExcel; # don't need this here, but might in real script to get cell addresses #use Spreadsheet::WriteExcel::Utility; my $workbook=Spreadsheet::WriteExcel->new("test-counts.xls"); my $files_worksheet=$workbook->addworksheet("Files"); $files_worksheet->write(0,0,"Filename"); $files_worksheet->write(0,1,"ErrorType"); my %errorCodes; my %errorCodesByFile; my ($row, $col); # let's assume there's an input file, with data like # one.cxx 588 # one.cxx 588 # ... open(INFILE,"<errorList") or die "Can't open errorList, error $!"; $row=1; while(<INFILE>) { my ($file, $err)=split; $files_worksheet->write($row,0,$file); $files_worksheet->write($row,1,$err); # we need the list of ALL the errors, to get column headings $errorCodes{$err}++; # we need to COUNT how often each error occurs in each file $errorCodesByFile{$file}->{$err}++; ++$row; } close(INFILE); my $summ_worksheet=$workbook->addworksheet("Summary"); $summ_worksheet->write(0,0,"fileName"); $summ_worksheet->write(0,1,"Error"); my @sortedCodes = sort keys %errorCodes; $col=2; foreach my $errCode(@sortedCodes) { $summ_worksheet->write(0,$col++,$errCode); } $row=1; foreach my $filename(sort keys %errorCodesByFile) { $summ_worksheet->write($row,0,$fileName); $col=2; foreach my $errCode(@sortedCodes) { # set to blank if value 0 or not defined my $count=$errorCodesByFile{$file}->{$errCode} || ''; $summ_worksheet->write($row,$col++,$count); } ++$row; }
    Hope this helps...
    --
    Mike
Re: WriteExcel
by thor (Priest) on Jun 23, 2002 at 05:46 UTC
    Well, this is not a Spreadsheet::WriteExcel specific answer, but here we go. If you are writing sheet one, you have access to all of the values, as you are probably iterating over them to write the sheet. If I were you, I would keep track of the values in a hash of hashes as I wrote them to sheet one, first keyed on file name, and then on error code. Then, to write sheet two, traverse your HoH and write the appopriate cells in to the sheet.

    thor