![]() |
|
The stupid question is the question not asked | |
PerlMonks |
RE:(4) Hash Nightmareby swiftone (Curate) |
on Oct 06, 2000 at 22:04 UTC ( #35629=note: print w/replies, xml ) | Need Help?? |
Okay, lets start with the basics then.
First, your code is bad practice. If you're new to Perl, you're excused, but we'll show you the Right Way (TM) :). The problem is that by having four different hashes, you data is separated. If you want to check the amount of memory left in a particular computer, what do you refer to? One of many psuedo-global variables. The better way to do it is to organize your data into one nested structure, that approaches the way we think of it. "All well and good," you say "but how do I do that?"
References. (This next section assumes you don't know about references. If you do, too bad, I'm writing it anyway :) )
For example, a hash can store only scalars, one for each key. But if a reference is a scalar, you could store a reference to, say, a list for each key of the hash. Suddenly your hash has (effectively) two layers. In the example you posted, my first solution was to create a list of references to your hashes. merlyn took this to an extreme and separated all your data into nested layers. (Which is good, because it means you can access any discrete piece of information) You can check out the syntax for creating and dereferencing references in perlref. Ignore all the sections about "soft" references, that's bad practice. Oh, and the obligatory newbie advice:
In Section
Seekers of Perl Wisdom
|
|