Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: the annoying keys "key" and "value"

by jabowery (Beadle)
on Dec 23, 2010 at 21:22 UTC ( [id://878882]=note: print w/replies, xml ) Need Help??


in reply to Re: the annoying keys "key" and "value"
in thread the annoying keys "key" and "value"

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re^3: the annoying keys "key" and "value"
by ikegami (Patriarch) on Dec 23, 2010 at 21:28 UTC

    An array of hashes whose elements are 'key' and 'value'.

    It preserves the order of the pairs, and allows for duplicate keys. This structure would be useful for storing an HTML form, for example.

    If you don't care about losing the order of the pairs, and you don't care about clobbering duplicate keys, you can flatten the structure using one of the following:

    my %flattened; for (@data) { $flattened{ $_->{'key'} } = $_->{'value'}; }
    my %flattened = map @{$_}{qw( key value )}, @data;
Re^3: the annoying keys "key" and "value"
by Marshall (Canon) on Dec 23, 2010 at 21:35 UTC
    I am not sure what you are getting at here. What you have presented is gobbledygook.

    The Array of Hash is the closest analogue to a simple 2-D array in C - it is used so that we don't have to define index 0 in an array means 'state' and index 1 in an array means 'location', i.e.

    #!/usr/bin/perl -w use strict; use Data::Dump qw[ pp ]; my @ArrayOfHash = ( { 'state' => 'TX', 'location' => 'US', }, { 'state' => 'Bavaria', 'location' => 'Germany', }, ); pp @ArrayOfHash; __END__ Prints: ( { location => "US", state => "TX" }, { location => "Germany", state => "Bavaria" }, )
    A 'C' array of struct is closer to AoH, but I still don't get your question. Explain where you have seen this.

    Using ordered tuples, would be an AoA, and expand back to the terms say 'state' or 'location' within a foreach loop or a map.

    #!/usr/bin/perl -w use strict; my @AoA = ( ['TX','US'], ['Bavaria','Germany'], ); foreach my $record_ref (@AoA) { my ($state, $location) = (@$record_ref); print "state = $state location=$location\n"; } __END__ prints: state = TX location=US state = Bavaria location=Germany
    Or
    foreach my $record_ref (@AoA) { my ($state, $location) = (@$record_ref); print "key = $state value=$location\n"; } __END__ key = TX value=US key = Bavaria value=Germany
    Again, not really sure what you are getting at.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-28 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found