That's where the | comes in. If you say

    my @b = (1, |@a, 2);

then you get (1, 'a', 'b', 'c', 2) like in Perl 5. But you can specify what
gets flattened, so you can choose to flatten some arrays but not others if
you need to for some reason:

    my @b = (1, |@b, 2, @b, 3);

gives you (1, 'a', 'b', 'c', 2, ('a', 'b', 'c'), 3). This often matters in
parameter lists, if you want one of the parameters to be the list itself
instead of it being spread across multiple parameters. In Perl 5 you had to
say \@b to pass a ref to the list instead, and the sub had to know it was
getting a scalar containing an arrayref and needed to dereference it (if
you don't know, don't ask; it's ugly).

On Fri, Oct 5, 2018 at 9:01 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> >> On Fri, Oct 5, 2018 at 7:44 PM ToddAndMargo via perl6-users
> >> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
> >>
> >>     On 10/5/18 3:15 PM, Ralph Mellor wrote:
> >>      > Well I guess my first way of explaining it was a complete bust.
> :)
> >>      >
> >>      > It's not going to be worth me discussing your reply to my first
> >>     attempt.
> >>
> >>     Hi Ralph,
> >>
> >>     Thank you!  I am going to have to save it for later and read it
> >>     over REAL SLOW!
> >>
> >>     :-)
> >>
> >>     -T
>
> On 10/5/18 5:50 PM, Brandon Allbery wrote:
> > You know how in perl 5, if you do:
> >
> >     my @a = qw(a b c);
> >     my @b = (1, @a, 2);
> >
> > @b will be (1, 'a', 'b', 'c', 2)? That's flattening. $b[1] will be 'a'.
> > Perl 5 doesn't really understand nested data structures, so you have to
> > simulate them with refs. If you use [@a] instead, $b[1] will contain an
> > arrayref that must be dereferenced to get at its contents. (Which it
> > tries to hide from you by letting you say $b[1][0] instead of
> > $b[1]->[0], which is what it's really doing. But if you say 'my $c =
> > $b[1]', you need $c->[0] to get its first item, not $c[0]. Refs are
> > better than what we had to do in Perl 4 with globs, but they're still
> > kinda icky.)
> >
> > Perl 6 understands nested lists and other data structures, and doesn't
> > flatten. If you do the above in Perl 6, you get (1, ('a', 'b', 'c'), 2)
> > with a nested list. @b[1] is ('a', 'b', 'c') and @b[1][0] is 'a', and
> > there's no hidden dereference operator involved, nor a "magic" arrayref
> > to be dereferenced.
> >
>
> Hi Brandon,
>
> I am following
>
>      my @a = qw(a b c);
>      my @b = (1, @a, 2);
>
>      @b is (1, ('a', 'b', 'c'), 2)
>
> So would flattening @b be (1 a b c 2) ?
>
> If I wanted to assign a flattened @b to @c, how would I do that?
>
> Thank you for the help!
>
> -T
>


-- 
brandon s allbery kf8nh
allber...@gmail.com

Reply via email to