[Chicago-talk] is this still on?
Shlomi Fish
shlomif at shlomifish.org
Mon May 18 03:13:50 PDT 2020
Hi Steven,
On Mon, 18 May 2020 01:30:36 -0500
Steven Lembark <lembark at wrkhors.com> wrote:
> 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 ) ];
>
You should use "(" instead of "[" and ")" instead of "]" there:
https://perl-begin.org/tutorials/bad-elements/#init_arrays_from_arrayrefs
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
>
--
Shlomi Fish https://www.shlomifish.org/
Humanity - Parody of Modern Life - https://shlom.in/humanity
I might be mad. But I’m a mad genius.
— https://www.shlomifish.org/humour.html
Please reply to list if it's a mailing list post - https://shlom.in/reply .
More information about the Chicago-talk
mailing list