Sign in to follow this  

[web] [Perl] reference to an array of hashes.... Help! (nevermind, I got it)

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Oke, here is the deal: I have a couple of arrays of hashes. something like:
my @array1 =
(
    { "who" => "me",    "age"  => "18" },
    { "who" => "other", "age"  => "17" }
);

my @array2 =
(
    { "who" => "someone", "age"  => "16" },
    { "who" => "anyone",  "age"  => "15" }
);

my $list = \@array1;





print @array1{"who"}; # works print $list{"who"} # doesn't How can I print something out of the array(1) using the $list reference?? (note: I know the example code sucks, but you get the idea.) edit:
foreach $tmp (@array1)  # so this works, but I need to use the refenence since I need to be able to use other arrays as well
{
    if ($content2 =~ $tmp->{"regexp"})
    {
        print "$tmp->{type}\n";
        ($object) = ($content2 =~ $tmp->{"object"});
        print "$object\n";
        last;
    }
    else
    {
        print "nooooooo\n";
    }
}


[Edited by - Medulla_Oblongata on September 17, 2004 3:48:05 AM]

Share this post


Link to post
Share on other sites
Nevermind, I found out how to do it.


my $ref = \@somearray; # reference to somearray
my $count = scalar(@$ref); # get the index count of in the array

for (my $i = 0; $i < $count; $i++) # loop through the array of hashes
{
# do stuff ...
print $ref->[$i]->{"who"}; # this is the way to access an element
# do stuff ...
}

Share this post


Link to post
Share on other sites

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this