SPUG: MORE INFO: Array Naming Question

North, Walter wnorth at state.mt.us
Wed Feb 4 11:19:36 CST 2004


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

Dan:

I collect some data from a storage array and read it into a hash
with the date as the key from which I then produce a graph for perusal
by management.

More storage arrays are on the way and I'd like to combine the data
however I haven't been able to come up with how to create a hash
with the storage array name as a hash that contains another hash
of the data keyed by date.

So I figured I could turn out multiple hashes with the date as
a key.

However I would prefer not to have to modify the script to add another
hash each time we get another storage device.  Hence I would want
to increment the name for example:

ST_ARRAY_1 and the next would be ST_ARRAY_2 etc.

A hash of hashes seems to be the superior method, but while I have
done hashes of arrays I cannot seem to get a satisfactory hash of hashes.

Hence my question.

As Christopher and Jeremy pointed out using a variable name for a variable
name is asking for trouble.


> -----Original Message-----
> From: Dan Ebert [mailto:mathin at mathin.com]
> Sent: Wednesday, February 04, 2004 9:40 AM
> Cc: spug
> Subject: RE: SPUG: Array Naming Question
> 
> 
> 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
> > 
> _____________________________________________________________
> 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