in reply to Help with references

As individual elements: $HASH{$key}[$index], as whole arrays: @{$HASH{$key}}, as all one list: map {@$_} @HASH{1..4}. See tye's References quick reference.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: Help with references
by ChrisR (Hermit) on Oct 23, 2003 at 03:06 UTC
    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?

      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

      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.