ventumsolve
asked on
advise on data-structure
I have to assign coordinates (right,left,top,bottom) to a number of objects residing in a 8 x 4 grid. First I wanted to create objects with get and set methods but cant find a way to name the objects the way I want to. I'm looping through the grid in two nested for-loops and wanted to create an object with the counter as an extension:
$asset_$counter = Asset->new();
That doesn't work because the variable-name is an address returned by the constructor.
So is this still the right way - OO? Would like that because it's handy. Or should I do something else .. e.g. creating a hash where each key points to an array of my values?
$asset_$counter = Asset->new();
That doesn't work because the variable-name is an address returned by the constructor.
So is this still the right way - OO? Would like that because it's handy. Or should I do something else .. e.g. creating a hash where each key points to an array of my values?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Yes that's good, I knew there was a way to get varaible variable names. My though was that having a lot of objects a1, a2, a3 etc is a bit redundant as the properties are so limited, just cordinates. With the data strucutre you just have a list of points which is easy to access and expand.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I should have mentioned that the assets stored in the grid might span over several rows and/or columns (but always coherent). And assets might have common boundary so they might share a xy-coordinate that should be (top,bottom) for the one but (0,0) for the other. That's why the oo-idea struck me as an appropriate datastructure. Furthermore each asset has other attributes besides from the coordinates that has to be accessed on different occassions.
This is by the way a script that should build an xml-file for InDesign and the grid-parser that I'm working on should build an image-tag and a story-tag for each asset.
Since you both opt for the array-array-solution I'll give that a go today to see if I can make it work.
This is by the way a script that should build an xml-file for InDesign and the grid-parser that I'm working on should build an image-tag and a story-tag for each asset.
Since you both opt for the array-array-solution I'll give that a go today to see if I can make it work.
If you use an array of hashes, your hash can have as many properties as you want
$asset>{'left'} = 1;
$asset->{'top'} = 12;
$asset->{'prop_1'} = 'x';
etc just add keys as you need them
$asset>{'left'} = 1;
$asset->{'top'} = 12;
$asset->{'prop_1'} = 'x';
etc just add keys as you need them
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanx, went for the hash-way but nice to know what best-practice on OO-perl would be.
ASKER
${asset.$counter} = Asset->new();