Jump to content
  • Advertisement
Sign in to follow this  
Guest Medulla_Oblongata

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

This topic is 5484 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

Guest Medulla_Oblongata
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
Guest Medulla_Oblongata
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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!