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

Hi, I'm creating a links page with the links sorted into categories. I think the best way of handling the information is as follows:
while (<FH>) { my ($webaddress, $sitename, $sitedesc, $category) = split "\t"; my @cc = ($webaddress, $sitename, $sitedesc); push @{ $sortmenu{$category} }, [$webaddress, $sitename, $sitedesc]; } close FH;
however I'm not clear on how to dereference the array. I wonder if someone could please explain what I need to do

Replies are listed 'Best First'.
Re: dereference an array
by Corion (Patriarch) on Oct 22, 2007 at 14:32 UTC

    See tye's References Quick Reference. Basically, if $arr is an array reference, @$arr gives you the referenced array and $arr->[42] gives you an element of it.

Re: dereference an array
by ikegami (Patriarch) on Oct 22, 2007 at 14:41 UTC

    You have a hash of references to arrays keyed by category.
    Each array is a list of records.
    Each record is a reference to an array with three elements (web address, site name and site desc).

    foreach my $category (sort keys %sortmenu) { foreach my $record (@{ $sortmenu{$category} }) { print "web address: $record->[0]\n"; print "site name: $record->[1]\n"; print "site desc: $record->[2]\n"; print "\n"; } }

    Data::Dumper may aid you in visualizing your data structure.

    use Data::Dumper; print(Dumper(\%sortmenu));
      That's a very nice explanation, thanks very much for your time - this is nearly on the verge of blowing a cerebral gasket!! :)
Re: dereference an array
by gamache (Friar) on Oct 22, 2007 at 15:07 UTC
    Reference an array (or anything else) by putting a backslash in front of it:
    push @{ $sortmenu{$category} }, \@cc;
    and dereference an arrayref by wrapping the it in @{}:
    my @a = @{$arrayref};