in reply to Spreadsheet::WriteExcel Loop problem

You should trim your code down to a minimum (you are improving) and make it stand alone so that we can reproduce your problem.

if ($j == 0) is always false because your loop starts with $j = 1.

If I edit your code to this:

use warnings; use strict; use Spreadsheet::WriteExcel; my @model = qw(x a b c d); my @devicesPerVariant = qw(x 2 1 3 1); my $rowCounter = 1; my $devicePerModel = 2; my $workbook = Spreadsheet::WriteExcel->new("test.xls"); my $worksheet = $workbook->add_worksheet(); my $j = 1; #Declare Headings for row 0 and the 6 Columns $worksheet->write(0,0 , 'Equipment Model'); $worksheet->write(0,1, 'Devices Per Model'); #Loop through Global Array called Models to determine the number of ro +ws to place data for (my $j = 1; $j <= $#model; $j++) { print "row is $rowCounter, $model[$j], $devicesPerVariant[$j]\n"; $worksheet->write($rowCounter,0, $model[$j]); if ($j == 0) { $worksheet->write($rowCounter,1, $devicePerModel); } $worksheet->write($rowCounter,2, $devicesPerVariant[$j]); $rowCounter++; } $workbook->close();

I get a spreedsheet containing:

Equipment Model Devices Per Model a 2 b 1 c 3 d 1

which is what I would expect to get.


Perl is Huffman encoded by design.

Replies are listed 'Best First'.
Re^2: Spreadsheet::WriteExcel Loop problem
by adamlee14 (Initiate) on Sep 16, 2005 at 05:21 UTC
    hi that work fine when you use static entrie in the array the thing is iam using global array that collect data first byway of reading a spreadsheet (Spreadsheet::ParseExcel) I have a function to do gather unique entries and put that data into lists. Then i have a function to do counting the number of unique entries that gives me with new data to place in a global array. Then i have a function in the script to go and write to a spread sheet by way of doing iterative loop on one specific array value that will keep a count on the number of rows to create. I can only get the results from the first set of data in the global arrays that are used to populate the spreadsheet. I dont know if i missing something in terms of how to use for loop and writing arrayi to spreadsheet, I have used print statememnt to see what i should be getting it just wont go into the spreadsheeet.

      Can you reduce you code to a stand alone sample, like the code I provided above, that demonstrates the problem? I strongly suspect that the bug has nothing to do with writing to the spreadsheet. however, until you can provide a complete sample that demonstrates the error I don't think that we can help you much more.

      It is quite likely that in the process of reducing your code to a minimum that demonstrates the problem, you will actually discover the bug.

      You haven't commented on the $j == 0 issue I mentioned


      Perl is Huffman encoded by design.