Question

In Perl, from the below hash:

{
    'item3' => {
        'floors' => [ 'a', 'b', 'c' ]
    },
    'item1' => [ 'special' ]
    'item2' => {
        'books' => {
            'topics' => [ 'epics' ]
        },
        'sports' => [ 'tennis' ]
    },
    'item5' => {
        'groups' => {
            'teams' => [ 'x', 'y' ]
        }
    },
    'offers' => {
        'list' => [ 'bags' ]
    }
}

Need to parse only last values in each sub level of hash where it is always be an array.

Only final child entries(array values) need to be listed.

Expecting the output in one single array:

[ 'a', 'b', 'c', 'special', 'epics', 'tennis', 'x', 'y', 'bags' ]

no idea how to proceed....please help me to get rid of this.

Advance Thanks!!!!

Was it helpful?

Solution

Recursion assumes that data structure consists of hashes and arrays only, and array elements are not references,

use strict;
use warnings;
use Data::Dumper;

my %h;
@h{ 1..5 } = (
    { 'item3' => { 'floors' => [ 'a', 'b', 'c' ] } },
    { 'item1' => [ 'special' ] },
    { 'item2' => {
                      'books' => { 'topics' => [ 'epics' ] },
                      'sports' => [ 'tennis' ]
                    }},
    { 'item5' => { 'groups' => { 'teams' => [ 'x', 'y' ] } } },
    { 'offers' => { 'list' => [ 'bags' ] } },
);

print Dumper [ arrvals(\%h) ];

sub arrvals {
  my ($ref) = @_;
  return ref($ref) eq "ARRAY"
    ? @$ref
    : map arrvals($_), values %$ref;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top