CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
access array of hashes



Mar 29, 2004, 12:43 AM

Post #1 of 2 (455 views)
access array of hashes Can't Post

I have a for each statement whic loops round and insert loads of hashes into an array like this:


my %entry = (href => $href, text => $label, bold => $bold);
push @output, \%entry;


$data = @output;

foreach my $l (@{ $data }) {


In the second for each how would i test to see if the first has contained an text value = "james"?

I have tried loads of combinations for accessing hashes in arrays like $l->{text} but nothing ?

Thaumaturge / Moderator

Mar 29, 2004, 5:45 AM

Post #2 of 2 (454 views)
Re: [novicemonkey] access array of hashes [In reply to] Can't Post

You don't have "use strict" turned on, do you? It's pretty much always a bad idea to write a Perl program without putting "use strict" at the top.

$data = @output;

What do you think that the line of code above does? It takes the number of element in @output and stores that number in $data;

I think that perhaps you might have meant:

$data = \@output;

Then you can do stuff like this:

foreach (@$data) { 
if ($_->{text} eq 'james') {
# you have found an element with a text item of 'james'

With "use strict" you would have got the error:

Can't use string ("x") as an ARRAY ref while "strict refs" in use at ./ line y.

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives