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

Re: unsure how to use variable

by oko1 (Deacon)
on Feb 17, 2012 at 17:02 UTC ( #954568=note: print w/ replies, xml ) Need Help??


in reply to unsure how to use variable

And then, there is always using a hash. Arrays are great, but you have to "shift" the count to fit, since array indices (in any sane language, anyway) always start at 0.

my %h = ( 1 => 'bleen', 2 => 'grue', 3 => 'smellow', ); for my $count (1..3){ print $h{$count}; }

(Amazing what you can learn here if you just ask the right kind of question, isn't it? :)

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


Comment on Re: unsure how to use variable
Download Code
Re^2: unsure how to use variable
by tobyink (Abbot) on Feb 17, 2012 at 18:14 UTC

    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.

      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
Node Status?
node history
Node Type: note [id://954568]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2014-12-27 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls