Reputation: 364
I have two hash, first one %hash contain uniq key and values.
The other one %channel contain only the Stores which are already present as values from %hash
I would like to display all the Category id (for example Category::1375567 etc..) which refer to the Store (for example Store::CAR)
Here below the expected result of what I need, it will be more efficient than my explication.
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'
);
Expected result
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
Actual result
Store::CAR
Store::CAR|Category::1375567|CAR Concert
Store::CAR|Category::1375568|Sciences
Store::CHANNEL|Category::1294556|Documentaire
Store::CHANNEL|Category::1299556|Culture
Store::CHANNEL|Category::1326557|Sport
Store::TV|Category::1314569|Séries & Fictions
Store::TV|Category::1314570|Info
Store::TV|Category::1314571|Emissions
Store::TV|Category::1314572|Jeunesse
Store::CHANNEL
Store::CAR|Category::1375567|CAR Concert
Store::CAR|Category::1375568|Sciences
Store::CHANNEL|Category::1294556|Documentaire
Store::CHANNEL|Category::1299556|Culture
Store::CHANNEL|Category::1326557|Sport
Store::TV|Category::1314569|Séries & Fictions
Store::TV|Category::1314570|Info
Store::TV|Category::1314571|Emissions
Store::TV|Category::1314572|Jeunesse
Here what I've done so far
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use feature 'say';
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'
);
if (%channel) {
foreach (sort keys %channel) {
say "\t", $_;
my @tab = grep{/'$_'/} sort values %hash;
if (@tab) {
foreach my $line (@tab) {
say $line;
}
say "";
}
}
}
#say Dumper(\%hash, \%channel);
__END__
Upvotes: 1
Views: 179
Reputation: 6798
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
Upvotes: 1
Reputation: 241758
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 "";
}
Upvotes: 2