develooper Front page | perl.perl6.users | Postings from April 2019

push() practice: misplaced semicolon creates list elements withinarray?

Thread Next
From:
William Michels via perl6-users
Date:
April 14, 2019 07:08
Subject:
push() practice: misplaced semicolon creates list elements withinarray?
Message ID:
CAA99HCw6u240CJa27Pm-BnubBAo25U43q9J9RvMDpCJfBHdxtg@mail.gmail.com
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.

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