SPUG: Array Naming Question

Dan Ebert mathin at mathin.com
Wed Feb 4 10:39:38 CST 2004


I curious to know what problem is trying to be solved by using a string
as the name of the variable.

Maybe you could use a hash where the string is the key and the value is
the array (or hash, or whatever)?

i.e.

my %names = (array1 => [1,2,3],
	      array2 => [4,5,6]);

my $use_this = 'array1';

foreach( @{ $names{$use_this} } ) { print; }



Another possibility could be to use references.

my @array1 = (1,2,3);
my @array2 = (3,4,5);

my $arrayref = \@array1;

my $use_this = 'array1';

if($use_this eq 'array2') { $arrayref = \@array2; }

foreach(@$arrayref) { print; }



On Wed, 2004-02-04 at 08:13, Cantrall, Christopher W wrote:
> Also, MJD has an article on this subject.  http://perl.plover.com/varvarname.html
> 
> 
> BeginQuote
> 
> The real root of the problem code is: It's fragile. You're mingling unlike things when you do this. And if two of those unlike things happen to have the same name, they'll collide and you'll get the wrong answer. So you end up having a whole long list of names which you have to be careful not to reuse, and if you screw up, you get a very bizarre error. This is precisely the problem that namespaces were invented to solve, and that's just what a hash is: A portable namespace.
> 
> ...
> 
> The real problem is that if your string contains something unexpected, it will sabotage a totally unrelated part of the program, and then you will have one hell of a time figuring out the bug.
> 
> EndQuote
> 
> 
> MJD has 3 articles on this.  Quite interesting.
> 
> HTH
> 
> ____________________________________________
> Chris Cantrall, Structural Engineer, 777
>     Christopher.W.Cantrall at Boeing.com
>   chris at cantrall.org
>     http://perlmonks.org/index.pl?node=Louis_Wu
>     http://spugwiki.perlocity.org/index.cgi?LouisWu
> 
> > -----Original Message-----
> > From: Jeremy G Kahn [mailto:kahn at cpan.org]
> > Sent: Wednesday, February 04, 2004 7:53 AM
> > To: North, Walter
> > Cc: spug
> > Subject: Re: SPUG: Array Naming Question
> > 
> > 
> > This is a good question, and a FAQ if you know where to look. The FAQ 
> > has an explanation for how you might want to go about doing this, and 
> > why it's probably a bad idea, and some suggestions for alternatives:
> > 
> > perldoc -q 'variable name':
> > 
> > Found in /usr/share/perl/5.8.2/pod/perlfaq7.pod
> >        How can I use a variable as a variable name?
> > 
> >                Beginners often think they want to have a variable
> >                contain the name of a variable.
> > 
> >                    $fred    = 23;
> >                    $varname = "fred";
> >                    ++$$varname;         # $fred now 24
> > 
> >                This works sometimes, but it is a very bad idea
> >                for two reasons.
> > 
> >                The first reason is that this technique only works
> >                on global variables.  That means that if $fred is
> >                a lexical variable created with my() in the above
> >                example, the code wouldn't work at all: you'd
> > ...
> > 
> > Hope that's useful.
> > 
> > 
> > --jeremy
> > 
> > North, Walter wrote:
> > 
> > >Good Morning all,
> > >
> > >Maybe this is a dumb question, but here goes anyway:
> > >
> > >Does anyone know if it possible to include a variable in, or 
> > use a variable
> > >as
> > >the name of an array or hash, and if so how would one do it?
> > >
> > >thanks in advance.
> > >
> > >
> > >----------------------------------------------------- 
> > >Walter North 406-444-2914 
> > >Operating Systems Programmer 
> > >wnorth (at) state (dot) mt (dot) us
> > >----------------------------------------------------- 
> > >
> > >_____________________________________________________________
> > >Seattle Perl Users Group Mailing List  
> > >POST TO: spug-list at mail.pm.org  http://spugwiki.perlocity.org
> > >ACCOUNT CONFIG: http://mail.pm.org/mailman/listinfo/spug-list
> > >MEETINGS: 3rd Tuesdays, U-District, Seattle WA
> > >WEB PAGE: http://www.seattleperl.org
> > >
> > >  
> > >
> > 
> > 
> > _____________________________________________________________
> > Seattle Perl Users Group Mailing List  
> > POST TO: spug-list at mail.pm.org  http://spugwiki.perlocity.org
> > ACCOUNT CONFIG: http://mail.pm.org/mailman/listinfo/spug-list
> > MEETINGS: 3rd Tuesdays, U-District, Seattle WA
> > WEB PAGE: http://www.seattleperl.org
> > 
> > 
> _____________________________________________________________
> Seattle Perl Users Group Mailing List  
> POST TO: spug-list at mail.pm.org  http://spugwiki.perlocity.org
> ACCOUNT CONFIG: http://mail.pm.org/mailman/listinfo/spug-list
> MEETINGS: 3rd Tuesdays, U-District, Seattle WA
> WEB PAGE: http://www.seattleperl.org
> 



More information about the spug-list mailing list