more map (+ Storable, Dumper)
my %alphabets = (
'AAA' => [ qw( aaa bbb ccc ) ],
'BBB' => [ qw( bbb ddd eee ggg iii ) ],
'CCC' => [ qw( fff ggg hhh ttt ) ],
);
my @fewer_than_4_items = grep @{ $alphabets{ $_ } } < 4, keys %alphabets;
my @list_has_bbb = grep {
my @list = @{ $alphabets{ $_ } };
grep $_ eq 'bbb', @list; # returns count of the items found
} keys %alphabets;
say "@fewer_than_4_items"; # AAA
say "@list_has_bbb"; # BBB AAA
my @remapped_ABC = map { # transforms hash to array below
[ $_ => $alphabets{ $_ } ]
# [ $_, $alphabets{ $_ } ] the same as above
} keys %alphabets;$
equals: @remapped_ABC = ( [ 'AAA', [ 'aaa' 'bbb' 'ccc' ]],
[ 'BBB', [ 'bbb', 'ddd', 'eee', 'ggg', 'iii' ]],
[ 'CCC', [ qw( fff ggg hhh ttt) ]], )
the following map transforms %alphabet to anonymous arrays of key-1value pairs:
my @ABC_with_items = map {
# saving $_ so that the inner map's $_ won't get messed up
my $abc = $_;
# saving list of items - visual aid
my @items = @{ $alphabets{$abc} };
# this map creates a list of anon arrays using
#+ the anonymous array constructor
map [$abc => $_], @items; # === map [$abc,$_], @items
} keys %alphabets;
we can dump the data with Data::Dumper or store/retrieve it from file:
use Data::Dumper;
print Dumper(\@ABC_with_items); # produces $VAR1 below
use Storable;
store \@ABC_with_items, 'ABCfile'; # stores variable in ABCfile (current dir)
# to retrieve it:
my $restored = retrieve 'ABCfile';
# to check retrieved vars:
$Data::Dumper::Purity = 1; # declare possibly self-referencing structs
print Dumper( $restored );
$VAR1 = [
[ 'CCC', 'fff' ],
[ 'CCC', 'ggg' ],
[ 'CCC', 'hhh'],
[ 'CCC', 'ttt' ],
[ 'BBB', 'bbb' ],
[ 'BBB', 'ddd' ],
[ 'BBB', 'eee' ],
[ 'BBB', 'ggg' ],
[ 'BBB', 'iii' ],
[ 'AAA', 'aaa' ],
[ 'AAA', 'bbb' ],
[ 'AAA', 'ccc' ]
];