develooper Front page | perl.perl6.users | Postings from November 2020

Re: The ,= operator

Thread Previous | Thread Next
From:
Joseph Brenner
Date:
November 30, 2020 17:32
Subject:
Re: The ,= operator
Message ID:
CAFfgvXVR3rE+Gh=ff1VE=htR_cuQB7Yxg5ZDCX+n9RAmA7_86Q@mail.gmail.com
William Michels <wjm1@caa.columbia.edu> wrote:

> Joe, what would you expect the code below to produce?

>  %h<> ,= c => 3;
>  @a[] ,= 'd';

Well *I* expect it to error out, but that's my p5 brain talking.

The Raku approach is if you ask for nothing it gives you
everything, so an empty index like that essentially doesn't
change anything.

And in any case, there are limits to how much it's worth
thinking about this case, because there are other ways to do what
I thought ,= would do on an array:

    my @a = <a b c>;
    @a.append('d');
    say @a; # [a b c d ]

As Ralph Mellor was suggesting, .append is indeed closer to what I was
thinking than .push.
When you're trying to add multiple elements, the .append method
appends the list to the list in the first array, but .push treats a
given array as a single element, creating a deeper data structure:

    my @n = <x y z>;
    @a.push( @n );      # treats array as a new element
    @a.append( @n );  # flattens the array, appends a list
    say @a;
    # [a b c d e [x y z] x y z]

Thread Previous | 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