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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



novicemonkey
Novice

Mar 29, 2004, 12:43 AM

Post #1 of 2 (378 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:

foreach(){

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 ?


davorg
Thaumaturge / Moderator

Mar 29, 2004, 5:45 AM

Post #2 of 2 (377 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.


Code
$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:


Code
$data = \@output;


Then you can do stuff like this:


Code
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 ./script.pl line y.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
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