http://qs321.pair.com?node_id=1231352


in reply to Hash filter one-liner not working as expected

Hi nysus,

I'm surprised you don't get an error, as I am getting this with your code:

syntax error at colors.pl line 31, near "$hash{"

Anyway, changing your code line with the grep and map as follows:

my %colors = map { $_ => $hash->{$_} } grep { $_ =~ /color$/ } keys %$ +hash;
or like this:
my %colors = map { $_ => $$hash{$_} } grep { $_ =~ /color$/ } keys %$h +ash;
works for me. Your error seems to be in this: %$hash{$_} (in the map).

Output of Data::Dumper on the %colors hash:

$ perl colors.pl $VAR1 = { 'test_menu_hl_color' => '#a74d3d', 'test_widget_color' => '#eee3b7', 'test_text_color' => '#000000', 'test_widget_header_color' => '#c6b989', 'test_menu_color' => '#004d59', 'background_color' => 'ffffff', 'test_article_background_color' => '#ceddd2', 'test_widget_title_color' => '#ffffff', 'test_menu_text_color' => '#ffffff', 'test_link_color' => '#31e500', 'test_headline_color' => '#7b0706', 'header_textcolor' => 'blank' };

Replies are listed 'Best First'.
Re^2: Hash filter one-liner not working as expected
by nysus (Parson) on Mar 17, 2019 at 10:40 UTC

    Yup, spotted my problem right after you posted. A brain fart that cost me 25 min of my life. Thanks.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      A brain fart that cost me 25 min of my life.
      And 10 of mine. ;-) But it was a pleasure to solve the problem, so I really can't complain about it. I like to solve problems far more than, say, playing a game on my computer.