Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: initializing multiple arrays

by mr.nick (Chaplain)
on May 30, 2014 at 20:32 UTC ( [id://1088068]=note: print w/replies, xml ) Need Help??


in reply to initializing multiple arrays

Just for fun, I was playing with creating interpolated variables and attempting to actually create a series of arrays called "@array00" .. "@array09". I thought that by using "use vars", "vars->import()" I could accomplish this with strict/warnings turned on.

This was my test:

#!/usr/bin/perl use strict; use warnings; use vars; my @array_references; for(0..9) { $array_references[$_] = [1..100]; } for(0..9) { my $aname = sprintf('array%02d', $_); vars->import("\@$aname"); @$aname = @{$array_references[$_]}; } print "@array01";

Which doesn't work. If I remove the strict/warnings section, it in fact does create the arrays and work as expected.

But how do you accomplish this (regardless of whether it's stupid or not) and keep warnings/strictness?

mr.nick ...

Replies are listed 'Best First'.
Re^2: initializing multiple arrays
by boftx (Deacon) on May 30, 2014 at 20:47 UTC

    You don't, unless you turn off strict refs:

    # since strict is a lexical pragma you can put it in a block and let # it return to its former state when you leave the block. { no strict refs; # do something that might be ill-advised ... }
    This is, in my experience, most commonly done to auto-generate accessor methods. But turning off strict refs is generally frowned upon. That is why I suggested using a hash to store the names as keys that can be computed.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1088068]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-23 18:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found