Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: One-liner to join keys on a two-dimensionnal hash ?

by LanX (Sage)
on Dec 29, 2021 at 15:31 UTC ( #11140011=note: print w/replies, xml ) Need Help??


in reply to One-liner to join keys on a two-dimensionnal hash ?

my solution:
use strict; use warnings; my %replaces = ( "foo" => { 'W100' => 'W102', 'W101' => 'W103', }, "bar" => { 'W120' => 'W99', 'W121' => 'W104', }, ); my $expr = join '|', map {keys %$_} values %replaces; print $expr;

(similar to ikegami's but DRYer)

There are multiple issues with your code:

  • your data structure was buggy since it swaps ( with {
  • your my $expr is scoped° to the loop, hence undef outside
  • $expr = will be overwritten with each iteration

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

°) kind of, strict doesn't complain.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11140011]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2023-05-31 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?