Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: unsure how to use variable

by tobyink (Canon)
on Feb 17, 2012 at 18:14 UTC ( [id://954579]=note: print w/replies, xml ) Need Help??


in reply to Re: unsure how to use variable
in thread unsure how to use variable

Actually, arrays don't always start at 0. If you set:

$[ = 1;

... and arrays suddenly start at 1. However (depending on what Perl version you use) $[ may be global, so set it in one place, and other code in distant modules will start acting strangely because they expect it to be set to 0. It has been made safer over time, but is still confusing. For that reason, $[ is being phased out in recent versions of Perl. 5.16 will drop support for $[ from the core code, but bundle an arybase module that implements it.

Either way, avoid $[.

Anyway, even if you prefer 1-based arrays over 0-based arrays, it's not a great idea to use hashes as an array substitute. For many cases (including this one), hashes are quite a bit slower.

Replies are listed 'Best First'.
Re^3: unsure how to use variable
by oko1 (Deacon) on Feb 18, 2012 at 04:54 UTC

    If we're going to nitpick: you're flat wrong in a number of places.

    • I never said that arrays start at 0. Indices, however, do - unless you change them.
    • Changing them via $[ won't make the arrays start at 1 either. Although the indices will start at whatever number is assigned to it.
    • Human language being the imperfect tool that it is, exceptions to "always" can always be found. Being able to do this is not a mark of ability, distinction, or anything else positive - especially when you end up qualifying your "correction" by saying "Either way, avoid [it]."
    • Your statement about hashes being slower is mostly misleading - badly so when trying to explain something to someone who is just learning the language. In fact, hashes are usually *much* faster than arrays - although, as always, there's the exception. Which you, of course, seized on.

    I'm totally fine - grateful, usually - when someone points out where I've been wrong; it's all a learning experience. Your post, however, was a pointless series of nitpicks and exception highlighting, and of no earthly use to anyone.

    -- 
    I hate storms, but calms undermine my spirits.
     -- Bernard Moitessier, "The Long Way"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found