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

Re: The ,= operator

Thread Previous | Thread Next
Joseph Brenner
November 30, 2020 17:32
Re: The ,= operator
Message ID:
William Michels <> 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>;
    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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About