Jump to content
  • Advertisement
Sign in to follow this  
Medulla_Oblongata

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

This topic is 5149 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!