con
con

Reputation: 6123

How to push onto a hash key of array?

I am trying to push onto a hash of an array in Perl6.

The perl5 equivalent is:

my %c;
@{ $c{'USA'} } = qw(Seattle Madison Bozeman Portland);
push @{ $c{'USA'} }, 'Philadelphia';

but this in Perl6:

my %c;
%c<USA> = 'Seattle', 'Madison', 'Bozeman', 'Portland';
%c{'USA'}.append: 'Philadelphia';

gives this error

Cannot call 'append' on an immutable 'List'

I get a similar error for Perl6's push, which would seem to be okay given the example from https://docs.perl6.org/routine/push which shows %h<a>.push(1); Trying %c<USA>.push('Philadelphia') also fails

what am I doing wrong here? I don't see this error on search engine results

Upvotes: 7

Views: 262

Answers (1)

Curt Tilmes
Curt Tilmes

Reputation: 3045

my %c;
%c<USA> = ['Seattle', 'Madison', 'Bozeman', 'Portland'];
%c{'USA'}.append: 'Philadelphia';

The brackets make an Array instead of a List

Links are to the Lists, sequences, and arrays docs which explain the difference, the primary being that List is immutable, while Array is not.

Upvotes: 11

Related Questions