On 10/5/18 8:49 PM, Brandon Allbery wrote:
Turns out I misspoke anyway; << >> isn't enough by itself.
$ p6 'my @x=<a b c>; my @y=<<1 2 @x 3 4>>; say @y; dd @y;'
@y will contain (1, 2, '@x', 3, 4). (Actually, the numbers will be
things that are simultaneously numbers and strings, showing up as
IntStr.new(1, '1') and such.) '@x' is a two-character string, not the
list @x or its contents.
pyanfar Z$ 6 'my @x = <a b c>; my @y = <<1 2 @x[] 3 4>>; dd @y'
Array @y = [IntStr.new(1, "1"), IntStr.new(2, "2"), "a", "b", "c",
IntStr.new(3, "3"), IntStr.new(4, "4")]
Which flattens because it's interpolating values, since quoting
constructs produce string-like things or lists of string-like things
(role Stringy), not lists of lists. If you want a list of lists, you are
better off just using lists directly, not the quoting constructs that
are intended to give you lists of strings. This is similar to Perl 5,
where qw() also doesn't expand variables:
pyanfar Z$ perl -MData::Dumper -E 'my @x = qw(a b c); my @y = qw(1
@x 2); say Dumper(\@y)'
$VAR1 = [
'1',
'@x',
'2'
];
On Fri, Oct 5, 2018 at 11:27 PM ToddAndMargo via perl6-users
<perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
On 10/5/18 6:28 PM, Brandon Allbery wrote:
> You can't with that, because < ... > acts like single quotes;
'@x' there
> is a string literal, not the list @x. If you use << ... >> then
it act
> like double quotes, and will use the list instead.
I am sorry. With the top posting, I can't tell
what you are talking about. I also can't tell which
of the three question I asked that you are
answering
:'(
>
> On Fri, Oct 5, 2018 at 9:23 PM ToddAndMargo via perl6-users
> <perl6-users@perl.org <mailto:perl6-users@perl.org>
<mailto:perl6-users@perl.org <mailto:perl6-users@perl.org>>> wrote:
>
> On 10/5/18 6:09 PM, Brandon Allbery wrote:
> > 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).
>
> Am I correct in my assumption?
>
> A little off the question, but how do I address something
> inside a nested array?
>
> In the following, how do I get at the "b"
>
>
> $ p6 'my @x=<a b c>; my @y=<1 2 @x 3 4>; say @y; dd @y;'
>
> [1 2 @x 3 4]
>
> Array @y = [IntStr.new(1, "1"), IntStr.new(2, "2"), "\@x",
> IntStr.new(3,
> "3"), IntStr.new(4, "4")]
>
> Also, how do I flatten @y above?
>
> Thank you for the help!
> -T
>
>
>
> --
> brandon s allbery kf8nh
> allber...@gmail.com <mailto:allber...@gmail.com>
<mailto:allber...@gmail.com <mailto:allber...@gmail.com>>
Hi Brandon,
I didn't think you'd ever expect to hear these words out of
my mouth, but I understand now. Thank you!
$ p6 'my @x=<a b c>; my @y=<<1 2 @x[] 3 4>>; dd @y; say @y; for @y.kv ->
$i,$j {say "Index=$i Value=$j"};'
Array @y = [IntStr.new(1, "1"), IntStr.new(2, "2"), "a", "b", "c",
IntStr.new(3, "3"), IntStr.new(4, "4")]
[1 2 a b c 3 4]
Index=0 Value=1
Index=1 Value=2
Index=2 Value=a
Index=3 Value=b
Index=4 Value=c
Index=5 Value=3
Index=6 Value=4
By the way, in P5, where I had to address elements of an array
with $[] use to DRIVE ME INSANE!
-T