Sun751 has asked for the wisdom of the Perl Monks concerning the following question:

if an array @arr holds reference of another array and hash for example,
@arr = (\@arrexample, \%hashexample);
how can I dereference @arr and assign @arrexample to @arrexample_copy out of @arr and assign %hashexample to %hashexample_copy out of @arr? any idea? please,
cheers, Updates..... As from above we know that @arr hold hashref and arrref.
I am looking forward to get hash and array that @arr hold in %hashexample_copy and @arrexample_copy.
  • Comment on array holding reference of hash and another array

Replies are listed 'Best First'.
Re: array holding reference of hash and another array
by moritz (Cardinal) on Sep 08, 2009 at 06:19 UTC
    This is explained very nicely in perlreftut.

    For example to get the keys of the hash, use:

    my @keys = keys %{ $arr[1] };
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: array holding reference of hash and another array
by dsheroh (Monsignor) on Sep 08, 2009 at 07:03 UTC
    You mean something like this? (untested code)
    for (@arr) { my @stuff = deref($_); do_stuff_with(@stuff); } sub deref { my $data = shift; if (ref $data eq 'ARRAY') { return @$data; } elsif (ref $data eq 'HASH') { return keys %$data; } else { warn "'$data' is not an array or hash reference!\n"; return; } }
Re: array holding reference of hash and another array
by cdarke (Prior) on Sep 08, 2009 at 08:26 UTC
    use strict; use warnings; my @arrexample = qw(The quick brown fox does stuff);; my %hashexample = qw(k1 v1 k2 v2 k3 v3); my @arr = (\@arrexample, \%hashexample);
    how can I dereference @arr

    @arr is not a reference, so you cannot dereference it. Its elements are references.

    assign @arrexample to @arrexample_copy out of @arr:
    my @arrexample_copy = @{$arr[0]};
    assign %hashexample to %hashexample_copy out of @arr:
    my %hashexample_copy = %{$arr[1]};