Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Builing a object name with for loop

by devbond (Novice)
on Aug 30, 2013 at 13:59 UTC ( #1051634=perlquestion: print w/replies, xml ) Need Help??
devbond has asked for the wisdom of the Perl Monks concerning the following question:

my $workbook = Spreadsheet::WriteExcel->new('perl.xls'); $s1 = $workbook->add_worksheet('cpze1'); $s2 = $workbook->add_worksheet('cpze2'); $s3 = $workbook->add_worksheet('cpze3'); $s4 = $workbook->add_worksheet('cpze4'); $s5 = $workbook->add_worksheet('cpze5'); $s6 = $workbook->add_worksheet('cpze6'); $s7 = $workbook->add_worksheet('cpze7'); $s8 = $workbook->add_worksheet('cpze8'); for ($i=1;$i<=8;$i++) { for ($r=0;$r<=24;$r++) { for ($c=0;$c<=4;$c++) { $s{$i}->write($r,$c,$arrays[$i][$r][$c]); } } }
I want to write values in all worksheets ,s1,s2,s3 ...s8 using a for loop the above code fails at $s{$i}.../I am not able to create object name with for loop variables. Please help

Replies are listed 'Best First'.
Re: Builing a object name with for loop
by hdb (Prior) on Aug 30, 2013 at 14:10 UTC

    If you use an array, things become much easier:

    my @s; $s[$_] = $workbook->add_worksheet( "cpze".$_ ) for 1..8; # $s[0] remains undefined! for my $i (1..8) { for my $r (0..24) { for my $c (0..4) { $s[$i]->write($r,$c,$arrays[$i][$r][$c]); } } }
Re: Builing a object name with for loop
by Corion (Pope) on Aug 30, 2013 at 14:01 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1051634]
Approved by hdb
[Corion]: 1nickt: No, I'm using it with Windows 10, but with the 4k display, yes. It's a very nice build IMO, and relatively sturdy. Also, it got 8 or 9 on the ifixit repair scale, which I consider good
[Corion]: ... especially as I expect to have to replace the battery set in two or three years' time
[1nickt]: Nice. So happy that Dells are still user-repairable. Unusual these days.
[1nickt]: I will certainly not be buying any more Macs.
[Corion]: 1nickt: Yeah, or at least, I hopoe so ;)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2017-03-27 13:15 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.