Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

variable in variables

by ohgary (Initiate)
on Apr 25, 2013 at 17:21 UTC ( #1030707=perlquestion: print w/replies, xml ) Need Help??
ohgary has asked for the wisdom of the Perl Monks concerning the following question:

I am lost in how to "variablize" this.

Working code:

use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("demo.xls"); my $worksheet_BOB = $workbook->add_worksheet('BOB'); my $worksheet_SUE = $workbook->add_worksheet('SUE'); $worksheet_BOB->write('A2', "Bob is here"); $worksheet_SUE->write('A2', "Sue is here"); __END__

What I want do do is make the _BOB a variable and loop through and array to build the worksheets. and then be able to reference the writes with the variable names.Below may not be syntactically correct but functional what I want to do

use strict; use Spreadsheet::WriteExcel; my $workbook = Spreadsheet::WriteExcel->new("demo.xls"); my @PEOPLE = ("BOB" , "SUE", "JOHN" ); foreach (@PEOPLE) { my $worksheet_$_ = $workbook->add_worksheet('$_'); } $NAME = "BOB" $worksheet_$NAME->write('A2', "$NAME is here"); $NAME= "SUE" $worksheet_$NAME->write('A2', "$NAME is here"); __END__

Replies are listed 'Best First'.
Re: variable in variables
by moritz (Cardinal) on Apr 25, 2013 at 17:27 UTC

    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'); }
      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?
        Old:
        $NAME = "BOB"; $worksheet_$NAME->write('A2', "$NAME is here");
        New:
        $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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1030707]
Approved by moritz
help
Chatterbox?
[choroba]: needed everywhere, available rarely
[LanX]: Question: starting a script from an icon in Windows, but after C-c the window closes ... what I want is to restart in the cmd.exe. Recommendations?
[Corion]: LanX: What do you mean by "restart in the cmd.exe" ?
[Corion]: Do you want to launch a script and keep the command prompt/console window open?
[Corion]: Do you want to wait for a key press before closing the window?
[LanX]: I want the command line in the history
[tye]: -Mouse
[Corion]: Option a) would mean launching cmd.exe /k c:\path\to\ batchfile- launching-perl- script.cmd. Option b) would be to add pause as the last line of said batch file.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2017-03-27 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.