in reply to Re: Help with references
in thread Help with references

As individual elements: $HASH{$key}[$index]
The sames values will be returned if you use $HASH{$key}->[$index]. I find this rather confusing. I looked at References quick reference and saw where tye said If the reference is in a hash or an array (and you are getting back a scalar), then you can drop the -> between the adjacent [0] and/or {KEY} parts. I'm just curious why this doesn't raise any sort of exception and if there is a correct or preferred version of the syntax?

Replies are listed 'Best First'.
Re: Re: Re: Help with references
by Zaxo (Archbishop) on Oct 23, 2003 at 03:13 UTC

    Either is correct. Since hash values must be scalars, perl can intuit the need to dereference for extra indexes or keys beyond the leftmost. Preference is strictly personal - use which syntax you like better.

    After Compline,
    Zaxo

Re: Re: Re: Help with references
by hanenkamp (Pilgrim) on Oct 23, 2003 at 04:05 UTC

    Preferred syntax? This is Perl, TMTOWTDI. That is, there is no preferred anything. There are, perhaps, caveats and some no-nos and some performance variations on occasion, but beyond this, do things the way you like. In this case, it's all syntactic sugar, so the interpreter ends up seeing the same thing whether there's a -> or not.

    Personally, I find that $HASH{$key}->[$index] has a higher noise-to-signal ratio than $HASH{$key}[$index], so I prefer the latter.