Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Slicing a hash reference from a collection of hash references within a hash reference

by nysus (Parson)
on Oct 07, 2015 at 15:21 UTC ( #1144070=perlquestion: print w/replies, xml ) Need Help??

nysus has asked for the wisdom of the Perl Monks concerning the following question:

This is an embarrassingly newbie question, but I have a mental block when it comes to references. Given the following simple data structure, how do I get a slice of the name data?
$household = { 23 => { last => 'Smith', first => 'Mary' }, 22 => { last => 'Jones', first => 'Bob' } };
Here's my newbie code that works:
my $key = 23; my $person = $household->{$key}; my %name = %$person; my @slice = @name{'first', 'last'};
How do I turn this into a one liner?

 

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

Replies are listed 'Best First'.
Re: Slicing a hash reference from a collection of hash references within a hash
by stevieb (Canon) on Oct 07, 2015 at 15:35 UTC

    First thing is, use strict; and use warnings;. There are syntax errors in the code you specified.

    Here's one way to extract out the hash slice you need...

    use warnings; use strict; my $household = { 23 => { last => 'Smith', first => 'Mary' }, 22 => { last => 'Jones', first => 'Bob' }, }; my $name = join ' ', @{ $household->{23} }{'first', 'last'};

    -stevieb

      Thank you! Looks so damn obvious once you see the solution.

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

Re: Slicing a hash reference from a collection of hash references within a hash reference
by toolic (Bishop) on Oct 07, 2015 at 15:36 UTC
    use warnings; use strict; use Data::Dumper; my $household = { 23 => { last => 'Smith', first => 'Mary' }, 22 => { last => 'Jones', first => 'Bob' } }; my @slice = @{ $household->{23} }{qw(first last)}; print Dumper(\@slice); __END__ $VAR1 = [ 'Mary', 'Smith' ];
Re: Slicing a hash reference from a collection of hash references within a hash reference
by johngg (Canon) on Oct 07, 2015 at 23:08 UTC
Re: Slicing a hash reference from a collection of hash references within a hash reference
by Anonymous Monk on Oct 07, 2015 at 17:11 UTC
    And as far as "getting your head around references" goes, just remember that they are a special type of scalar that "refers to" something else. Anything can be referred-to, and everything has a reference-count so that it will not disappear until all references are gone. (Mumble about "weakened" references ...) And, as far as "in one line" goes, don't be too concerned about that. It is not "a sign of a newbie" to use more source-code lines.
      It is not "a sign of a newbie" to use more source-code lines.

      To the contrary, indeed. Just recently, I was hacking together a piece of code that featured a map within a map. Sure, the whole thing fitted on one "line", if your definition of "line" is somewhat liberal:

      $some_object->one_method->other_method( [map { ... } map {...} @$sourc +e_aref] )

      Which, in reality, looked more like:

      $some_object->one_method->other_method( [ map { ... } map { ... } @$source_aref ]);

      But then I was looking at it, and I shook my head. Sure, it worked. But was it "clean" code? Not by a long stretch: I could either slap multiple lines on comments on it, explaining what was going on, or refactor the whole thing:

      use subs qw( fribble_aref frobble_aref ); my $results_aref = fribble_aref frobble_aref $source_aref ; my $other_object = $some_object->one_method; $other_object->other_method( $results_aref ); sub fribble_aref { return [map { ... } @{+shift}]; } sub frobble_aref { return [map { ... } @{+shift}]; }

      The final solution definitely took more lines, but it also was way more comprehensible.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1144070]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2021-04-19 16:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?