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

Re: push() practice: misplaced semicolon creates list elementswithin array?

Thread Previous | Thread Next
From:
William Michels via perl6-users
Date:
May 1, 2019 18:43
Subject:
Re: push() practice: misplaced semicolon creates list elementswithin array?
Message ID:
CAA99HCxgHP12LiM45RBAM-QYdM17w00APTRzq3cSuXdCft=wpw@mail.gmail.com
Hello Richard and thank you for your comment,

I'm not sure I have clarity on the use of semicolons in Perl 6. Most
references in the documentation refer to the semicolon as a statement
separator. Many references point out where semicolons are optional
(e.g. particular lines within blocks).

Now for multi-dimensional arrays, I see commas being used as
'dimensional-separators", and semicolons used to slice a
multi-dimensional array.

From RosettaCode:

# Multi dimension arrays may be predeclared which constrains the
indices to the declared size:
>my @dim5[3,3,3,3,3];

#Creates a preallocated 5 dimensional array where each branch has 3
storage slots and constrains the size to the declared size.
#It can then be accessed like so:
@dim5[0;1;2;1;0] = 'Perl 6';
say @dim5[0;1;2;1;0]; # Perl 6

https://rosettacode.org/wiki/Multi-dimensional_array#Perl_6

Seems to be different from the push() issue. --B.


On Sun, Apr 14, 2019 at 12:39 AM Richard Hainsworth
<rnhainsworth@gmail.com> wrote:
>
> A semicolon is the syntax used for multidimensional arrays.
>
> See https://docs.perl6.org/language/subscripts#Multiple_dimensions
>
>
> On 14/04/2019 15:07, William Michels via perl6-users 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[dot]youtube[dot]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 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