Paul Kraus wrote:
>
> I think I am picking up some bad habits so maybe you guys can help.
>
> I currently use this notation @{$hash{key}} to access an array stored in
> a hash.
> I keep seeing posts that it is better to use an object like notation.
> Can I see some examples on how you would access the array or an element
> of an array.
>
> Also is there a way to access slices in a foreach something like
>
> Foreach ((@arrar)[1,3..6]){
> code
> }

Hi Paul.

By 'object-like notation' I think you mean the arrow operator?

First, be careful to forget that it's object-like at all! If $ar
is an array reference then the whole array is @{$ar} and the first
element is ${$ar}[0] or $ar->[0]. Often (like here) you can miss out
the braces, so the array is @$ar and the element is $$ar[0].

$hash{key} is an array reference, so you can do the same with that
hash element as you can with a scalar.

  @{$hash{key}};      # is the array

  ( @$hash{key} won't work here because it's ambiguous )

and

  ${$hash{key}}[0];    # is the element
or
  $hash{key}->[0];

Because the indirection arrow between pairs of brackets (suqare or curly)
can be removed, the last can be reduced to.

  $hash{key}[0];

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to