in reply to Get all hash value into array

Here's the most simple solution I found... The power of perl :) :)

#!/usr/bin/perl use utf8; use Modern::Perl; use Data::Dumper; my $db = { '99155' => { 'PR' => [ 'state_name=Puerto Rico', 'county_names_all=Adjuntas|Utuado', ], 'AK' => [ 'state_name=Alaska', 'county_names_all=Ketchikan Gateway|Prince of + + Wales-Hyder', ], 'WA' => [ 'state_name=Washington', 'county_names_all=Pend Oreille|Spokane|Lincol + +n|Adams', 'comments=America/Los_Angeles' ] }, '26134' => { 'WV' => [ 'state_name=West Virginia', 'county_names_all=Wirt|Wood|Jackson|Ritchie| + +Calhoun', 'comments=America/New_York' ] } }; my %states = map { ((split /=/, $$_[0], 2)[1], 'hurray!') } map { values %$_ } values %$db; print "states are: ", (join "; ", sort, keys %states), "\n";

result is:

root@orion:/tmp# ./mytest.pl states are: West Virginia; Puerto Rico; Washington; Alaska

Replies are listed 'Best First'.
Re^2: Get all hash value into array
by LanX (Saint) on Feb 02, 2020 at 13:07 UTC
    > most simple solution

    Simplest? I doubt that, but it's maybe the shortest.

    And your solution depends on the assumption that the array fields have always a fixed index.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Yess... if data structures are more structured then program gets more safe and precise:

      #!/usr/bin/perl use utf8; use Modern::Perl; use Data::Dumper; my $db = { '99155' => { 'PR' => { state_name => 'Puerto Rico', county_names_all => [qw{Adjuntas Utuado}], }, 'AK' => { state_name => 'Alaska', county_names_all => ['Ketchikan', 'Gateway', +'Prince of + Wales-Hyder'], }, 'WA' => { state_name => 'Washington', county_names_all => ['Pend Oreille', 'Spokane +', 'Lincol +nAdams'], comments => 'America/Los_Angeles' } }, '26134' => { 'WV' => { state_name => 'West Virginia', county_names_all => ['Wirt', 'Wood', 'Jackso +n', 'Ritchie', 'Calhoun'], comments => 'America/New_York' } } }; my %states_with_comments = map { ($$_{state_name}, 'hurray!') } grep { $$_{comments} } map { values %$_ } values %$db; print "states are: ", (join "; ", sort, keys %states_with_comments), " +\n";

      Dumper of db:

      $VAR1 = { '26134' => { 'WV' => { 'county_names_all' => [ 'Wirt', 'Wood', 'Jackson', 'Ritchie', 'Calhoun' ], 'comments' => 'America/New_York', 'state_name' => 'West Virginia' } }, '99155' => { 'WA' => { 'comments' => 'America/Los_Angeles', 'state_name' => 'Washington', 'county_names_all' => [ 'Pend Oreille +', 'Spokane', 'Lincol +nAda +ms' ] }, 'AK' => { 'county_names_all' => [ 'Ketchikan', 'Gateway', 'Prince of + +Wales-Hyder' ], 'state_name' => 'Alaska' }, 'PR' => { 'state_name' => 'Puerto Rico', 'county_names_all' => [ 'Adjuntas', 'Utuado' ] } } };