Just in case it wasn't clear from Bill's discussion, I think the
reason this case seems weird is normally we expect trailing separators
to be ignored, as with the extra comma here:

   > my @monsters = ('ghidora', 'mothera', 'wolfman', 'zuckerberg',);
  [ghidora mothera wolfman zuckerberg]

If you do this, and type in a trailing semi-colon in the wrong place,
you stumble across some odd behavior:

   > @monsters.push('tingler';)
  [ghidora mothera wolfman zuckerberg (tingler) ()]

It seems a bit LTA, though it might be unavoidable because this is valid:

   my @monsters = ('ghidora', 'mothera'; 'frankenstein',
'wolfman';'purple-people-eater')
   [(ghidora mothera) (frankenstein wolfman) purple-people-eater]

Though even here, the behavior seems a bit strange, because
"purple-people-eater" ended up as an element in the top-level list.
Hm, no empty list is appended if you do this:

  my @monsters = ('ghidora', 'mothera'; 'frankenstein', 'wolfman';)
  [(ghidora mothera) (frankenstein wolfman)]

But the push method seems to interpret it differently:

  @monsters.push('purple-people-eater';)
  [(ghidora mothera) (frankenstein wolfman) (purple-people-eater) ()]



On 4/14/19, William Michels via perl6-users <perl6-us...@perl.org> wrote:
> Hello,
>
> I've been working through Patrick Michaud's excellent videos from the
> The Perl Conference 2016. At about 35:45 of the following 2016 video
> (Part 1 of 2), Patrick discusses arrays:
>
> https://www.youtube.com/watch?v=ySch4xpoPA0
>
> At this point in the video, Patrick also discusses push() and pop()
> calls on arrays. For practice I tried pushing and popping strings in
> the REPL. However, I discovered an unusual property when I misplaced a
> semicolon during call to push(). See what happens below when a
> semicolon is included within the parentheses of push():
>
> "This is Rakudo version 2018.12 built on MoarVM version 2018.12
> implementing Perl 6.d."
>
>> my @countries = "UK", "Spain", "Slovakia", "Sweden";
> [UK Spain Slovakia Sweden]
>> @countries.push("Finland");
> [UK Spain Slovakia Sweden Finland]
>> my @countries = "UK", "Spain", "Slovakia", "Sweden";
> [UK Spain Slovakia Sweden]
>> @countries.push("Finland";)
> [UK Spain Slovakia Sweden (Finland) ()]
>> my @countries = "UK", "Spain", "Slovakia", "Sweden";
> [UK Spain Slovakia Sweden]
>> @countries.push("Finland";);
> [UK Spain Slovakia Sweden (Finland) ()]
>
> Misplacing a semicolon within the push() call adds two elements to the
> array. When I examine these two elements, I see that they are both
> "List" elements:
>
>> @countries[3].WHAT
> (Str)
>> @countries[4].WHAT
> (List)
>> @countries[5].WHAT
> (List)
>
> Apparently, multiple semicolons within push() will add multiple list
> elements to the end of the intended array:
>
>> my @countries = "UK", "Spain", "Slovakia", "Sweden";
> [UK Spain Slovakia Sweden]
>> @countries.push("Finland";;);
> [UK Spain Slovakia Sweden (Finland) () ()]
>> my @countries = "UK", "Spain", "Slovakia", "Sweden";
> [UK Spain Slovakia Sweden]
>> @countries.push(;;;;;;;);
> [UK Spain Slovakia Sweden () () () () () () () ()]
>
> It is surprising to me that "List" elements are appended to the array
> with push() as described above. If one tries to add one or more
> elements via indexing and there 'aren't enough elements' so to speak
> (by accident or design), the array grows by inserting "Any" elements,
> not "List" elements:
>
>> my @countries = "UK", "Spain", "Slovakia", "Sweden";
> [UK Spain Slovakia Sweden]
>> @countries[5] = "Finland";
> Finland
>> say @countries
> [UK Spain Slovakia Sweden (Any) Finland]
>>
>> my @countries = "UK", "Spain", "Slovakia", "Sweden";
> [UK Spain Slovakia Sweden]
>> @countries[6..7] = "Finland", "Norway";
> (Finland Norway)
>> say @countries
> [UK Spain Slovakia Sweden (Any) (Any) Finland Norway]
>
> I've briefly checked pop() to see if there are similar issues, but 1)
> placing a string within the parentheses of pop() will throw an error,
> and 2) placing a semicolon within the parentheses of pop() will throw
> an error. However, these error message are slightly different. A
> string argument to pop() will result in an error that says "Too many
> positionals passed; expected 1 argument but got 2" while a semicolon
> argument to pop() will result in an error that says "Too many
> positionals passed; expected 1 argument but got 3".
>
>> my @countries = "UK", "Spain", "Slovakia", "Sweden";
> [UK Spain Slovakia Sweden]
>> @countries.pop("Finland")
> Too many positionals passed; expected 1 argument but got 2
>   in block <unit> at <unknown file> line 1
>
>> @countries.pop(;)
> Too many positionals passed; expected 1 argument but got 3
>   in block <unit> at <unknown file> line 1
>
>>
>
>
> Any help appreciated,
>
> Bill.
>
> William Michels, Ph.D.
>
> PS Thanks to Joe Brenner for talking over this Perl6 code with me.
>

Reply via email to