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

My apologies !!

Original content (below) and title restored by GrandFather.

Hi,

First here is the related snippet of code I want to ask your assistance for :

my $row=1; while ($a = $sth->fetchrow_hashref()) { $worksheet->write_url($row,0, qq({internal:'$a->{OrderID}'!A1})); $worksheet->write($row,0, $a->{OrderID}, $bold); $worksheet->write($row,1, $a->{OrderDate}); $worksheet->write($row,2, $a->{ShippedDate}); $worksheet->write($row,3, $a->{CustomerName}); $worksheet->write($row,4, $a->{ShipVia}); $worksheet->write($row,5, $a->{ShipName}); $worksheet->write($row,6, $a->{ShipAddress}); $worksheet->write($row,7, $a->{ShipCity}); $worksheet->write($row,8, $a->{ShipRegion}); $worksheet->write($row,9, $a->{ShipPostalCode}); $worksheet->write($row,10, $a->{ShipCountry}); $worksheet->write($row,11, $a->{Freight},$number); $freight = $a->{Freight}; $ordttl = $a->{'Total'} + $a->{'Freight'}; $worksheet->write($row,12,$ordttl,$number); $row++; print "Current row number is $row\n";

After $row++ loop how do I maintain the current $row number so I can add other stuff like grand total to be positioned just after $row++ loop in the same worksheet.

As you can see above I am printing the current $row after $row++ loop and it shows that current row is 2 this is not I want, I want the next row after $row++ loop, I hope I have made my question clear enough.

Many many thanks for your help !!

Terry

Replies are listed 'Best First'.
Re: Spreadsheet::WriteExcel how to maintain row number
by GrandFather (Saint) on Jul 29, 2014 at 01:30 UTC

    You have at least three options: 1/ reorder the lines so the increment is after the print, 2/ save the $row value prior to increment, 3/ use printf and post increment $row in the argument list.

    # 1/ print "Current row number is $row\n"; ++$row; # 2/ my $oldRowValue = $row; ++$row; print "Current row number is $oldRowValue\n"; # 3/ printf "Current row number is %d\n", $row++;

    Note that pre (++$row) and post ($row++) increment are different. Post increment behaves like 2/ - the original value is saved, the variable is incremented and the saved value is returned. Pre increment simply increments the variable then returns the result. If your skin gets itchy when you see needless inefficiency then you'll probably avoid post increment ($row++) where a pre increment works just as well, especially as shoving the increment operator out the front makes it easier to see something special is going on.

    Perl is the programming world's equivalent of English
Re: Sorry I wanted to delete it
by Anonymous Monk on Jul 28, 2014 at 20:41 UTC
      Well, given that there are no replies yet .... go ahead and let the OP delete the node, there are no replies to it

        That wasn't the point. From the linked page:

        Why shouldn't I delete a node or its content?

        PerlMonks is a place to learn. As often happens in real life, Perl Monks learn (and often learn best) from mistakes that they and others have made. If you make a mistake in a node and then delete it, you may be saving your pride but you are also depriving your fellow Monks of the chance to learn from the mistake. Only actual duplicates, spam, and obvious trolling should be deleted.

        It looks like the OP didn't give the question very long to garner helpful replies and the OP doesn't say why the node was scrubbed. There was opportunity for (I think) a useful reply to the OP's question. I don't remember the topic of pre/post increment being dealt with in reply to a question and it is a potential trap for newbies so I've added a reply to that effect (and restored the node content to give the original context).

        Perl is the programming world's equivalent of English