Perl how can I get values from a hash with key from another hash

Solution 1:

You can remove some redundancy to make the hashes smaller.

Also, add utf8 as the script contains the character é; to get it printed correctly, add open.

Transform the first hash so that you key the values by the common value. As there are multiple values per this transformed key, use a hash of arrays.

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

use utf8;
use open OUT => ':encoding(UTF-8)', ':std';

my %hash = (
        '1375567' => 'Store::CAR|CAR Concert',
        '1299556' => 'Store::CHANNEL|Culture',
        '1314571' => 'Store::TV|Emissions',
        '1375568' => 'Store::CAR|Sciences',
        '1314570' => 'Store::TV|Info',
        '1314572' => 'Store::TV|Jeunesse',
        '1314569' => 'Store::TV|Séries & Fictions',
        '1294556' => 'Store::CHANNEL|Documentaire',
        '1326557' => 'Store::CHANNEL|Sport'
);

my %channel;
@channel{qw{ Store::TV Store::CAR Store::CHANNEL }} = ();

my %transformed;
for my $category (keys %hash) {
    my ($val1, $val2) = split /\|/, $hash{$category};
    push @{ $transformed{$val1} }, [$category, $val2];
}

for my $ch (sort keys %channel) {
    say ' ' x 4, $ch;
    local $" = '|';
    say "Category::@$_" for sort { $a->[0] <=> $b->[0] } @{ $transformed{$ch} };
    say "";
}

Solution 2:

Following demo code creates new hash reference with $stores where key is store id and values copied from %hash.

Using keys from hash %channel output data from hash reference with $stores

#!/usr/bin/env perl

use strict;
use warnings;
use feature 'say';

use utf8;

my $stores;
my %hash = (
        'Category::1375567' => 'Store::CAR|Category::1375567|CAR Concert',
        'Category::1299556' => 'Store::CHANNEL|Category::1299556|Culture',
        'Category::1314571' => 'Store::TV|Category::1314571|Emissions',
        'Category::1375568' => 'Store::CAR|Category::1375568|Sciences',
        'Category::1314570' => 'Store::TV|Category::1314570|Info',
        'Category::1314572' => 'Store::TV|Category::1314572|Jeunesse',
        'Category::1314569' => 'Store::TV|Category::1314569|Séries & Fictions',
        'Category::1294556' => 'Store::CHANNEL|Category::1294556|Documentaire',
        'Category::1326557' => 'Store::CHANNEL|Category::1326557|Sport'
);

my %channel = (
        'Store::TV' => 'Store::TV',
        'Store::CAR' => 'Store::CAR',
        'Store::CHANNEL' => 'Store::CHANNEL'
);

for ( values %hash ) {
        my($id,$cat,$desc) = split /\|/;
        push @{$stores->{$id}}, $_;
}

for my $id ( sort keys %channel ) {
        say ' ' x 4 . $id;
        say $_ for @{$stores->{$id}};
        say '';
}

exit 0;

Output

    Store::CAR
Store::CAR|Category::1375567|CAR Concert
Store::CAR|Category::1375568|Sciences

    Store::CHANNEL
Store::CHANNEL|Category::1294556|Documentaire
Store::CHANNEL|Category::1299556|Culture
Store::CHANNEL|Category::1326557|Sport

    Store::TV
Store::TV|Category::1314569|Séries & Fictions
Store::TV|Category::1314570|Info
Store::TV|Category::1314571|Emissions
Store::TV|Category::1314572|Jeunesse