Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: variable in variables

by moritz (Cardinal)
on Apr 25, 2013 at 17:27 UTC ( #1030708=note: print w/replies, xml ) Need Help??

in reply to variable in variables

The proper way to do this is to use a hash, not variable variable names:

use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("demo.xls"); my %worksheets; my @PEOPLE = ("BOB" , "SUE", "JOHN" ); for my $p (@PEOPLE) { $worksheets{$p} = $workbook->add_worksheet($p); $worksheets{$p}->write('A2', ucfirst(lc($p)) . ' is here'); }

Replies are listed 'Best First'.
Re^2: variable in variables
by ohgary (Initiate) on Apr 25, 2013 at 20:41 UTC
    Ok, that handles an initialization of the worksheet, but how do I do the worksheets writes outside of a for loop?
    An adhoc write to another element?
    As my example, How do I assign a "person" and then be able to do my write as my example showed?

      how do I do the worksheets writes outside of a for loop?
      $NAME = "BOB"; $worksheet_$NAME->write('A2', "$NAME is here");
      $NAME = "BOB"; $worksheets{$NAME}->write('A2', "$NAME is here");

      BTW, as for why symbolic references should be avoided, see the classic three part series by MJD: part 1 and part 2 and part 3.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1030708]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-03-22 23:59 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (286 votes). Check out past polls.