in reply to Array indices

I think people here lost track of the fact that your example is only contrived. I do agree with them that your example is a good one to show where you wouldn't need indices if you were clever in the first place.

Sometimes, it is true, you can't do without an index. However, a lot of the time you can. Consider "lot" to be something in the 95% range. Very rarely is there a need to access elements by index with Perl's innate understanding of what a list is.

Basically, it boils down to data structure layout. There's a saying by one of the CS greats along the lines of:

Show me your flow charts, but conceil your data structures. I will be mystified. Show me your data structures, but withhold your flow charts, and I won't need them for they will naturally follow.

(I'd be eternally grateful if someone could identify the originator and tell me the exact phrasing, btw.)

You may want to have a look at Dominus' Program Repair Shop and Red Flags article series on Perl.com for some practical but generalized advice.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: Array indices
by jordanh (Chaplain) on Sep 14, 2002 at 16:24 UTC
    I really agree with this.

    "If you are good, you never need to use indices." is an over-generalization. And, you know what they say about generalizations. Generalizations are always wrong. :-)

    That being said, it's really pretty rare that you need to use indices in Perl. As many have pointed out, your example is not a good one.

    Here are a few cases where I can imagine that indices are very useful:

    • Picking a random element from a list. Monte Carlo algorithms, genetic algorithms and skip lists come to mind here.

    • They are more or less necessary in numerical coding, such as linearlization, Simpson's method, Trapezoidal approximation, FFT, various methods of numerical approximation.

      Perl is probably a sub-optimal language choice for most of these uses, but you might use it for prototyping and pedagogical reasons.

    • Other math programming, like number theory, perhaps.

    • They might come in handy in agorithms where you are dealing with tuples of data, such as points in a triangle, n points evenly distributed taken from a set, etc.

    • Reviewing some code I've recently written, sometimes, I'll have used a routine that will return a list and I happen to know that in this context it will be a list of one element. Probably should always test the idea that it's not possible to get more than one returned (or less than one!), but you know, lazyness... In those cases, I've just picked off the zeroeth element. There might be a better way to do this, though. I could pop it or shift it, too, for example. Maybe this is an example that's acceptable sometimes. I'd have to think about this.

    • When using things like HTML::Tokeparser, you see a lot of code that accesses the zeroeth element of the returned lists, because this is a specialized tag that determines how to use the rest of the returned list. I think this is probably clearer than shifting it off. OTOH, it might be better to shift it off and send the rest of the list to specialized processing routines based on the type of data. I'd have to think about this one, too.

    Most of these examples are fairly unusual. In some of them, you could probably find ways to do it without indices, but indices are the most natural way of doing it. I guess I would say that using the zeroeth element is a special case that comes up a lot and may be acceptable.

    In languages like C, the use of indices is more pervasive. This is because C doesn't have good high-level data structures. It's often convenient in C to maintain lists as arrays because more complicated structures will have to be garbage collected and you have to be very careful about what you do with references to such structures.

    You don't have those problems in Perl. So, you should try to fit your solution into high level data structures, where access via indices is unnatural, for a number of reasons. Understandability, maintainability and extensibility being a few.

    Array indices in Perl should be usually viewed as a low-level feature. To be used very sparingly and usually encapsulated as much as possible so that the main program logic doesn't have to deal with them. Viewed this way, most of the 0/1-based arguments become trivialized. Who cares if they are 0 or 1 based as long as they are used so rarely?

    Array indices are similar to goto, in many respects. Rarely necessary, but just the thing for some situations.

    Update: September 15, 2002 1107 EDT In reviewing some code, it's pretty clear to me that I overstated the case against indices above. I also think that it's valid to use array indices to access arguments to subroutines, to access any data returned as fixed vectors, like from local/gmtime() and the like. These might roughly fit into the category above of accessing tuples, but it's actually a lot more common than I have presented it as being. In any case, their use is still fairly limited. These cases are "read only" kind of uses, where the data is presented in a list or array and you just want to fetch specific fields.

Re: Re: Array indices
by sauoq (Abbot) on Sep 15, 2002 at 18:08 UTC
    (I'd be eternally grateful if someone could identify the originator and tell me the exact phrasing, btw.)

    That was Fred Brooks. In chapter nine of the The Mythical Man Month, he wrote, "Show me your flowcharts and conceal your tables, and I shall continue to be mystified. Show me your tables, and I won't usually need your flowcharts; they'll be obvious."

    Thanks for asking. It had been too long since I cracked the cover on this classic.

    -sauoq
    "My two cents aren't worth a dime.";