ivbtar
ivbtar

Reputation: 879

Iterating on hash keys in perl

I have a hash like below.

(
    'cars' => {
        'citroen' => {
            'sedan'    => { 'color' => 'red'   },
            'hatchback => { 'color' => 'white' },
            'truck'    => { 'color' => 'black' },
        },
        'ford' => {
            'sedan'    => { 'color' => 'red'   },
            'hatchback => { 'color' => 'white' },
            'truck'    => { 'color' => 'black' },
        },
    },
)

Car versions are sedan, hatchback and truck I want to get these versions and iterate over them. If sedan do.... If hatchback do...... If truck do....

I tried:

foreach my $key ( keys %{ $myhash->{'cars'}->{$carCompany} } ) { 
    print "\nCarVersion:" . $key;
}

But I couldn't get these versions.

Upvotes: 1

Views: 173

Answers (1)

ivbtar
ivbtar

Reputation: 879

I solved it.

foreach my $key (keys %{$myhash->{'cars'}->{'ford'}}) { print $key."\n"; }

this gives:

sedan
hatchback
truck

This was the output I wanted.

Upvotes: 1

Related Questions