Hi all, First of, I'm relatively new at perl so if my question sounds stupid or my code doesn't make sense to you, you will understand why. I wrote the following code to parse Microsft Excel files in a certain directory, and generate a summary file. The problem is the code run really very slow. I mean it takes hours on a my powerbook g4 550, with 512 ram. and pretty nothing else is runnnig. I'm not sure why this is the case. I would really appreciate it if someone can point out to me what things I'm doing which might be slowing down the code. Thanks in advance.
#!/usr/bin/perl use strict; $|++; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; my %heading = ( bold => 1, pattern => 1, fg_color => 15, border => 1, align => 'center', ); my @filesToBeProcessed = <INC.*.xls>; my $file; my @arrayOfData; my $totalDayMinutes = 0; my $totalNightMinnites = 0; my $totalGSMMinutes = 0; for (my $x = 0; $x <= $#filesToBeProcessed; $x++) { my $oExcel = Spreadsheet::ParseExcel->new(); my $oBook = $oExcel->Parse($filesToBeProcessed[$x]); my $oWkS = $oBook->{Worksheet}[1]; my @data = getFieldData($oWkS); $data[0] =~ s/\'//gm; $arrayOfData[$x] = [ @data ]; $totalDayMinutes = $totalDayMinutes + $data[1]; $totalNightMinnites = $totalNightMinnites + $data[2]; $totalGSMMinutes = $totalGSMMinutes + $data[3]; } my $workbook = Spreadsheet::WriteExcel->new("summary.xls"); my $worksheet = $workbook->add_worksheet("Summary"); my $heading = $workbook->add_format(%heading); my $format = $workbook->add_format(); my @fields =("Date", "Day Minutes", "Night Minutes", "GSM"); for (my $x = 0; $x < @fields; $x++) { $worksheet->set_column(0, $x, 15); $worksheet->write(0, $x, $fields[$x], $heading); } $format->set_bold(); $format->set_color('black'); $format->set_align('center'); for (my $i = 0; $i <= $#arrayOfData; $i++) { for (my $j = 0; $j < $#{$arrayOfData[$i]}; $j++) { $worksheet->write(($i + 1), $j, $arrayOfData[$i][$j], $form +at); } } $worksheet->write(($#arrayOfData + 2), 1, $totalDayMinutes, $format); $worksheet->write(($#arrayOfData + 2), 2, $totalNightMinnites, $format +); $worksheet->write(($#arrayOfData + 2), 3, $totalGSMMinutes, $format); $workbook->close(); ###################### # Subroutines ###################### sub getFieldData { my ($workSheet) = $_[0]; my @array; for (my $x = 0, my $y = 0; $x <= $workSheet->{MaxRow}; $x++, $y++) { my $cell = $workSheet->{Cells}[$x][1]; $array[$y] = $cell->Value,if($cell); } return @array; }

Title edit by tye


In reply to Slow Code, Spreadsheet::ParseExcel, and Spreadsheet::WriteExcel by Idris

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.