I'll give your idea a closer look. Have found a way though to name my objects;
${asset.$counter} = Asset->new();
Main Topics
Browse All TopicsI 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?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Why do you want the actual variable name to change? Although you can do this, I don't think this is a very good design, as it'll make other things difficult.
If you want to store things in a grid, an array-of-array makes sense. You would access things like:
$grid[$row][$col]
Or even as a hash:
$grid{"$row.$col"}
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.
The suggestion, by Adam314, to not use variably named variables is a good suggestion. And, if you have 'use strict' on (and you do 'use strict', right...) then you won't be able to use variables in that way.
If you want to use an OO approach, and have all the Accessor and and Getter methods maintained automagically for you, use inside-out objects from Class::Std (i highly recommend them for all/most OO perl programing). Below is a simple class setup and sample script that will do what we are talking about here. Read the Class::Std man page and/or Perl Best Practices to find out how to write custom methods and such...
david
Business Accounts
Answer for Membership
by: teraplanePosted on 2009-10-06 at 14:57:15ID: 25510347
I think your idea of using a hash sounds better and would make the code more readable. Perhaps an array of hashes. So you have
while () { # loop to extract data
$point = {}
$point->{'left'} = 1;
$point->{'top'} = 12;
etc
push @array_of_points,$point;
}