[Chicago-talk] is this still on?

Steven Lembark lembark at wrkhors.com
Sun May 17 23:30:36 PDT 2020


On Sun, 17 May 2020 15:01:45 -0400
James E Keenan <jkeenan at pobox.com> wrote:

> On 5/17/20 12:49 PM, Jay S wrote:
> > Hi Perl mongers, I'm not sure if this list is still on|active.
> > I hope everyone is doing well and staying sane.
> > I haven't programmed in a long time and can't remember stuff.  How
> > do make a list out of a hash?
> > 
> > Instead of:
> > my $ClubID = $data{ClubID};
> > my $GameCode = $data{GameCode};
> > my $DateStarted = $data{DateStarted};
> > my $GameType = $data{GameType};
> > 
> > I'd like to do something like this but can't remember the proper 
> > incantation:
> > my ($ClubID, $GameCode, $DateStarted, $GameType) = @data(qw[ClubID 
> > GameCode DateStarted GameType]);

You are looking for a "hash slice":

Quick refresher: The data type you are extracting from is specified
by curly or square braces:

  foo{ ... }  access foo as a  hash
  foo[ ... }  access foo as an an array

You are extracting a list of values (vs. key+value pairs) which 
leaves you with:

    @foo{ ... }

to pull out the values of interest.

qw takes open-close pairs, I tend to prefer parens since they look 
look more "list-ish" to me and doesn't get mistaken for an arrayref.

Leaves:

    my ($ClubID, $GameCode, $DateStarted, $GameType) 
    = @data{ qw( GameCode DateStarted GameType ) };

or 
    my @keyz = [ qw( GameCode DateStarted GameType ) ]; 

    ...

    my ($ClubID, $GameCode, $DateStarted, $GameType) = @data{ @keyz };

or

    state $keyz = [ qw( GameCode DateStarted GameType ) ]; 

    ...

    my ($ClubID, $GameCode, $DateStarted, $GameType) = @data{ @$keyz };


Note that perl recently added a "kv-slice" which returns the 
keys and values using a '%' sigil instead of '@':

    my ($ClubID, $GameCode, $DateStarted, $GameType) = %data{ @keyz };

this would provide a list of key-value pairs suitable for assigning
to a new hash (if you saw that while experimenting you mistyped a 
'%' instead of a '@'.

Zei gesund

-- 
Steven Lembark
Workhorse Computing
lembark at wrkhors.com
+1 888 359 3508


More information about the Chicago-talk mailing list