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

hi i have a text file with comma a delimiter. i have extracted the data. But i need to write the data to a excel file, using loop. If i give numbers for the cell, the code works. If i use a variable like $s_counter, it throws an error. Followin is the code.

until ($s_counter == $permanent_counter) { $Sheet->Cells(1,1)->{Value} = "somevalue"; $s_counter=$s_counter+1; }

If i use

$Sheet->Cells($s_counter,1)->{Value} = "somevalue";

instead of

$Sheet->Cells(1,1)->{Value} = "somevalue";

it does not work. can someone help me

20060216 Janitored by Corion: Moved from Perl Monks Discussion to Seekers of Perl Wisdom, added formatting

Replies are listed 'Best First'.
Re: unable to use interpolation in for loop for writing in excel
by Corion (Patriarch) on Feb 16, 2006 at 08:16 UTC

    I see no error in the snippet you posted. Most likely your error is that $s_counter already has a value larger than $permanent_counter before you hit your loop, so your loop never gets executed.

    Using lexical variables avoids the problem of forgetting to initialize loop counters. The strict pragma tells Perl that you want to pre-declare all variables:

    use strict; my ($s_counter); until ($s_counter == $permanent_counter) { $Sheet->Cells(1,1)->{Value} = "somevalue"; $s_counter=$s_counter+1; }

    Alternatively, you can continue with the problematic programming style you currently have, by explicitly initializing $s_counter before your loop:

    $s_counter = 0; until ($s_counter == $permanent_counter) { $Sheet->Cells(1,1)->{Value} = "somevalue"; $s_counter=$s_counter+1; }
Re: unable to use interpolation in for loop for writing in excel
by Anonymous Monk on Feb 16, 2006 at 08:12 UTC

    Its working!! Please Check it once again.