Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Array indices

by mattr (Curate)
on Sep 14, 2002 at 15:05 UTC ( [id://197869]=note: print w/replies, xml ) Need Help??


in reply to Array indices

In short, "Tastes great!" vs. "Less filling!". I don't think your logic is flawed, and it is perfectly fine to use indices and loop through an array in Perl.

If you are writing for other people, it's nice to be elegant but better to be clear and bulletproof. No law says you must do X here.

You will notice most of the answers include looping. You can get out of doing it more than once by investing intelligence and cpu time into your data structure the first time you see the data, long before you need to search it. For example, you might read off your answers as all the elements in the anonymous array pointed to by the hash key 'heimer'. Another way would be to keep track of the number of "heimer"s you found and make hash keys heimer_1, heimer_2, .. heimer_n which could be a quick addition to an existing system. (Maybe another way, inverse hash lookup, or whatever it's called will arrive in Perl 6.)

It's like making a secondary key in the database world. The moral of the story is there is no evil (well maybe some, (somebody's evil twin brother plus the problems that disappeared when I removed the distant Switch code from my app) - but certainly not here) in Perl. As a number of people tried to say, being smart about data structures is a Good Thing. But being too complicated and dogmatic can be a Bad Thing, especially if it isn't comfortable.

Why the rant? I just finished building a simple little photo album and I had the funniest thing happen when I changed from using consecutive numbers to using a config file that lists all the image filenames and their captions. As it happens it was very easy to fix and was not due to keeping track of the index (which I do need to know, to jump to image 5 for example). But it was still fun. I dunno, in high school data structures they ragged on me and called me Mr. Index cause I liked index files so much (Basic IV (?), remember? Massive 10MB disk the size of a super size stuffed pizza in 1984 - all ascii, so that was unimaginable space!) Anyway, works for me. My code for my brother Bob's photos of Iceland, Norway, and Parts East for what it's worth. I think he takes good photos! Also looking for a new challenge - was founder of bangnetworks.com. The third photo's my fave. Best of luck. -Matt R.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://197869]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-03-19 08:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found