Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Structure Problem

Status
Not open for further replies.

rotis23

Programmer
Aug 29, 2002
121
GB
Hi All,

Wonder if you can help me because I'm getting bogged down with this.

I have an application written in Perl that includes several modules and they all reference a Constants.pm module for obvious reasons.

Now, I have some constants that are more complex than just a number and I've been trying to use them as hashes like:

Code:
use constant RESULT_STRUCTURE => {
        VALUE           => 10,
        MORE_DATA       => "Human readable data.",
};

This is so that I don't use multiple arrays that reference each other.

The problem is that I need to assign this data structure to an object attribute so I don't have to keep referencing arrays or the constants.

So, a couple of questions:

- should I be using a complex constant like this? Is there a better way? It seems too complex.

- I assume I need to pass a reference to the structure to the object attirbute. I haven't figured out how to reference a reference to a constant hash yet??!! Any ideas?

Does this make sense?

Thanks for any help,

rotis23



 
What I really need to do is to grab a value from the data structure based on another value. e.g.:

Get MORE_DATA from the data structure where VALUE = 10.

I know I can do this using an array and it's index, but the values aren't sequential from 0.

I guess the other alternative is to search through the collection looking at each key? Seems a big job just to get some simple relational data.

rotis23
 
Hello rotis,

If I understand your needs correctly it sounds as if you need to use a hash, an associative array, which are just like an ordinary arrays except that they are indexed by strings - like this:

$hDescriptions{'PaulTeg'} = 'Perl God";
$hDescriptions{'MikeLacey'} = 'Devilishly good looking";
$hDescriptions{'mikevh'} = 'Way too clever";

This also works:

$hYourData{10} = 'some string you want to store';
$hYourData{26} = 59.942;


Mike

To err is human,
but to really foul things up -
you require a man Mike.

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top