Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to access info from the array

by gnangia (Scribe)
on Nov 12, 2002 at 20:57 UTC ( [id://212410]=perlquestion: print w/replies, xml ) Need Help??

gnangia has asked for the wisdom of the Perl Monks concerning the following question:

Could somebody please explain to me how one would access information from the following -
push (@{$data{$formname}}, { 'type' => "select", 'field_name' => $selectname, 'value' => $value, });

Replies are listed 'Best First'.
Re: How to access info from the array
by arturo (Vicar) on Nov 12, 2002 at 21:20 UTC

    General answer : References quick reference.

    More specific answer: $data{$formname} is an entry in a hash. The wrapping of the @{ ... } around it indicates that this hash entry is a reference to an array. What's being pushed onto the array is an anonymous hash (reference to a hash with no name).

    Putting that together listily:

    • $data{$formname} => reference to an array
      • dereference with @{ REFERENCE }
    • $data{$formname}->[0] => the first element of that array. Perl lets you tighten that up to $data{$formname}[0]. The element of that array is a reference to a hash, so
      • dereference with %{ REFERENCE }
    • $data{$formname}->[0]->{type} (also $data{$formname}[0]{type} ) => The value of the 'type' field in the anonymous hash. This is a normal scalar, and so needs no dereferencing.

    So, to iterate over the keys of that (anonymous) hash, you might do

    foreach my $key ( keys %{ $data{$formname}[0] } ) { print $key, " ", $data{$formname}[0]{$key}, "\n"; }

    HTH

    If not P, what? Q maybe?
    "Sidney Morgenbesser"

      Thank-you. That little tutorial was very useful.
      Thanks also to everyone who replied.
        The Data::Dumper module and its ilk in the Data:: namespace on CPAN might be useful also.
Re: How to access info from the array
by broquaint (Abbot) on Nov 12, 2002 at 21:07 UTC
    Something like
    foreach my $i (0 .. $#{ $data{$formname} }) { print "$_: $data{$formname}->[$i]->{$_}\n" foreach keys %{ $data{$formname}->[$i] }; }
    See. perlreftut for more info.
    HTH

    _________
    broquaint

Re: How to access info from the array
by revdiablo (Prior) on Nov 12, 2002 at 21:09 UTC

    What you have here is a hash (indexed by form name), containing arrays of hashes. You can loop on the main hash, pulling out the specified fields from each hash in the array. For example:

    while (my ($formname, $formarray) = each %data) {
        print $formname, "\n";
    
        foreach (@$formarray) {
            print "  type:       ", $_->{type},       "\n";
            print "  field_name: ", $_->{field_name}, "\n";
            print "  value:      ", $_->{value},      "\n";
        }
    }
    

    Note: as broquaint mentioned, you should see perlreftut. I also find perldsc quite helpful at times.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://212410]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-18 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found