develooper Front page | perl.perl5.porters | Postings from August 2020

push HASH, LIST

Thread Next
From:
Eric Brine
Date:
August 4, 2020 21:01
Subject:
push HASH, LIST
Message ID:
CALJW-qH4jMVekRQZ-YLi9bMB9pDVaa9gH5CFm3p6JrW2TS1nQg@mail.gmail.com
We now have key-value slices.

    $ perl -e'%h=(a=>6,b=>7,c=>8); CORE::say for %h{qw( a b )};'
    a
    6
    b
    7

So we can easily retrieve a list of key-value pairs. Adding key-value pairs
to a hash, on the other hand, isn't as nice. Do so requires

    my %kvpairs = @kvpairs;
    @h{ keys(%kvpairs) } = values(%kvpairs);

or

    use List::Util qw( pairkeys pairvalues );

    @h{ pairkeys(@kvpairs) } = pairvalues(@kvpairs);

or

    %h = ( %h, @kvpairs );

None of those are great. Would there be any problems with supporting

    push %hash, @kvpairs;

and of course

    push $hash->%*, @kvpairs;

These are currently compilation errors. These do not have the same problem
as `push REF, LIST` had (which was that certain references can be
considered both a hash and an array).

- Eric "ikegami" Brine

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About