SPUG: MORE INFO: Array Naming Question

Brian Maddux brianmaddux at yahoo.com
Wed Feb 4 12:00:58 CST 2004


I would suggest getting the Advanced Perl Programming
book from O'Reilly. It really helps with hashes,
references, etc.

Once you get the concept down, its easy to make data
structures to fit almost any need.

It also has a (small) section on OO Perl and making
your own modules.

This book has greatly expanded my abilities for
solving problems with Perl.


Brian
--- "North, Walter" <wnorth at state.mt.us> wrote:
> 
> Dan:
> 
> >>>>>>>Blinding Flash of Light<<<<<<<<<
> 
> Sometimes the obvious is just too
> obvious.............
> 
> Its the key stupid, not the hash name...........
> 
> Thank you.
> 
> Also Chris Wilkes for the thought on creating my own
> perl module.  That is definately one I had not
> thought of.
> 
> Perhaps as my skills increase........
> 
> thanks to all
> 
> > -----Original Message-----
> > From: Dan Ebert [mailto:mathin at mathin.com]
> > Sent: Wednesday, February 04, 2004 10:31 AM
> > To: North, Walter
> > Cc: Spug (E-mail)
> > Subject: Re: SPUG: MORE INFO: Array Naming
> Question
> > 
> > 
> > 
> > Could you make the key like this:
> > 
> > my $base = 'storage_array_';
> > 
> > my $number = 1; # this can be the part that can be
> dynamic
> > 
> > my $key = $base . $number;
> > 
> > $hash{$key} = %date_based_hash;
> > 
> > On Wed, 2004-02-04 at 09:19, North, Walter wrote:
> > > > 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 
> 
=== message truncated ===


=====
-------------------------------------------------------
Brian Maddux                    Whidbey Island, WA
brianmaddux at yahoo.com   
-------------------------------------------------------

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/



More information about the spug-list mailing list