\n and array question

Hi All,

Just out of curiosity, why is the \n printed out literally here?

$ alias p6
alias p6='perl6 -e'

p6 'my @x = <"aaa\n","bbb\n","ccc\n">; for @x {print @_};'

"aaa\n","bbb\n","ccc\n"


Many thanks,
-T

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
perl6
11/14/2020 7:02:14 PM
perl.perl6.users 1504 articles. 0 followers. Follow

8 Replies
10 Views

Similar Articles

[PageSpeed] 25

On Sat, Nov 14, 2020 at 2:03 PM ToddAndMargo via perl6-users
<perl6-users@perl.org> wrote:
> Just out of curiosity, why is the \n printed out literally here?
> p6 'my @x = <"aaa\n","bbb\n","ccc\n">; for @x {print @_};'

Your 'word quoting' <> is sort of like single quotes -- it keeps the
literal stuff.  You could
use <<>> which is more like double quotes,

Curt
0
curt
11/14/2020 7:08:55 PM
On 2020-11-14 11:08, Curt Tilmes wrote:
> On Sat, Nov 14, 2020 at 2:03 PM ToddAndMargo via perl6-users
> <perl6-users@perl.org> wrote:
>> Just out of curiosity, why is the \n printed out literally here?
>> p6 'my @x = <"aaa\n","bbb\n","ccc\n">; for @x {print @_};'
> 
> Your 'word quoting' <> is sort of like single quotes -- it keeps the
> literal stuff.  You could
> use <<>> which is more like double quotes,
> 
> Curt
> 

or remove the commas.  I put everything in [0]


$ p6 'my @x = <aaa\n bbb\n ccc\n>; for @x {print "$_\n";}'
aaa\n
bbb\n
ccc\n

$ p6 'my @x = <<aaa\n bbb\n ccc\n>>; for @x {print "$_\n";}'
aaa
bbb
ccc

$ p6 'my @x = <<aaa\n bbb\n ccc\n>>; for @x {print "$_";}'
aaabbbccc

What am I missing?

-T

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
perl6
11/14/2020 7:12:46 PM
The <=E2=80=A6> and =C2=AB=E2=80=A6=C2=BB constructors break on =
whitespace.

So <a,b,c,d,e,f> will actually produce the following array:

    ["a,b,c,d,e,f"]

It's only one item.  If we placed space after the comma, that is, <a, b, =
c, d, e, f>, you'd get a six item list, but with the commas attached to =
all but the final:

   ["a,", "b,", "c,", "d,", "e,", "f"]

By replacing the commas with spaces, e.g., <a b c d e f>, you allow it =
to break into ["a", "b", "c", "d", "e", "f"]

Mat=C3=A9u

> On Nov 14, 2020, at 14:12, ToddAndMargo via perl6-users =
<perl6-users@perl.org> wrote:
>=20
> On 2020-11-14 11:08, Curt Tilmes wrote:
>> On Sat, Nov 14, 2020 at 2:03 PM ToddAndMargo via perl6-users
>> <perl6-users@perl.org> wrote:
>>> Just out of curiosity, why is the \n printed out literally here?
>>> p6 'my @x =3D <"aaa\n","bbb\n","ccc\n">; for @x {print @_};'
>> Your 'word quoting' <> is sort of like single quotes -- it keeps the
>> literal stuff.  You could
>> use <<>> which is more like double quotes,
>> Curt
>=20
> or remove the commas.  I put everything in [0]
>=20
>=20
> $ p6 'my @x =3D <aaa\n bbb\n ccc\n>; for @x {print "$_\n";}'
> aaa\n
> bbb\n
> ccc\n
>=20
> $ p6 'my @x =3D <<aaa\n bbb\n ccc\n>>; for @x {print "$_\n";}'
> aaa
> bbb
> ccc
>=20
> $ p6 'my @x =3D <<aaa\n bbb\n ccc\n>>; for @x {print "$_";}'
> aaabbbccc
>=20
> What am I missing?
>=20
> -T
>=20
> --=20
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Computers are like air conditioners.
> They malfunction when you open windows
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
mateu
11/14/2020 7:18:28 PM
 >> On Nov 14, 2020, at 14:12, ToddAndMargo via perl6-users=20
<perl6-users@perl.org> wrote:
 >>
 >> On 2020-11-14 11:08, Curt Tilmes wrote:
 >>> On Sat, Nov 14, 2020 at 2:03 PM ToddAndMargo via perl6-users
 >>> <perl6-users@perl.org> wrote:
 >>>> Just out of curiosity, why is the \n printed out literally here?
 >>>> p6 'my @x =3D <"aaa\n","bbb\n","ccc\n">; for @x {print @_};'
 >>> Your 'word quoting' <> is sort of like single quotes -- it keeps the=

 >>> literal stuff.  You could
 >>> use <<>> which is more like double quotes,
 >>> Curt
 >>
 >> or remove the commas.  I put everything in [0]
 >>
 >>
 >> $ p6 'my @x =3D <aaa\n bbb\n ccc\n>; for @x {print "$_\n";}'
 >> aaa\n
 >> bbb\n
 >> ccc\n
 >>
 >> $ p6 'my @x =3D <<aaa\n bbb\n ccc\n>>; for @x {print "$_\n";}'
 >> aaa
 >> bbb
 >> ccc
 >>
 >> $ p6 'my @x =3D <<aaa\n bbb\n ccc\n>>; for @x {print "$_";}'
 >> aaabbbccc
 >>
 >> What am I missing?
 >>
 >> -T


On 2020-11-14 11:18, Matthew Stuckwisch wrote:
> The <=E2=80=A6> and =C2=AB=E2=80=A6=C2=BB constructors break on whitesp=
ace.
>=20
> So <a,b,c,d,e,f> will actually produce the following array:
>=20
>      ["a,b,c,d,e,f"]
>=20
> It's only one item.  If we placed space after the comma, that is, <a, b=
, c, d, e, f>, you'd get a six item list, but with the commas attached to=
 all but the final:
>=20
>     ["a,", "b,", "c,", "d,", "e,", "f"]
>=20
> By replacing the commas with spaces, e.g., <a b c d e f>, you allow it =
to break into ["a", "b", "c", "d", "e", "f"]
>=20
> Mat=C3=A9u
>=20

Ya, I caught that booboo.  :'(

Question still stands.  Why is the \n working as a CR/LF and
being printed as a litteral?
0
perl6
11/14/2020 7:21:12 PM
--0000000000008c9e6a05b416a24f
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

    <a b c>

is the same as

    Q :single :words < a b c >

Note that :single means it acts like single quotes.

Single quotes don't do anything to convert '\n' into anything other than a
literal '\n'.

If you want that to be converted to a linefeed you need to use double quote
semantics (or at least turn on :backslash).

    Q :double :words < a\n b\n c >

Of course that also doesn't do what you want because a linefeed character
is also whitespace, so it gets removed along with the rest of the
whitespace.

What you want to do use is :quotewords and "".

    Q :quotewords < "a\n" "b\n" c >

The short way to write that is

    << "a\n" "b\n" c >>

Although if you are going to append a newline to every element I would
consider writing it this way:

    < a b c > X~ "\n"

or

    < a b c > =C2=BB~=C2=BB "\n"

On Sat, Nov 14, 2020 at 1:21 PM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

>  >> On Nov 14, 2020, at 14:12, ToddAndMargo via perl6-users
> <perl6-users@perl.org> wrote:
>  >>
>  >> On 2020-11-14 11:08, Curt Tilmes wrote:
>  >>> On Sat, Nov 14, 2020 at 2:03 PM ToddAndMargo via perl6-users
>  >>> <perl6-users@perl.org> wrote:
>  >>>> Just out of curiosity, why is the \n printed out literally here?
>  >>>> p6 'my @x =3D <"aaa\n","bbb\n","ccc\n">; for @x {print @_};'
>  >>> Your 'word quoting' <> is sort of like single quotes -- it keeps the
>  >>> literal stuff.  You could
>  >>> use <<>> which is more like double quotes,
>  >>> Curt
>  >>
>  >> or remove the commas.  I put everything in [0]
>  >>
>  >>
>  >> $ p6 'my @x =3D <aaa\n bbb\n ccc\n>; for @x {print "$_\n";}'
>  >> aaa\n
>  >> bbb\n
>  >> ccc\n
>  >>
>  >> $ p6 'my @x =3D <<aaa\n bbb\n ccc\n>>; for @x {print "$_\n";}'
>  >> aaa
>  >> bbb
>  >> ccc
>  >>
>  >> $ p6 'my @x =3D <<aaa\n bbb\n ccc\n>>; for @x {print "$_";}'
>  >> aaabbbccc
>  >>
>  >> What am I missing?
>  >>
>  >> -T
>
>
> On 2020-11-14 11:18, Matthew Stuckwisch wrote:
> > The <=E2=80=A6> and =C2=AB=E2=80=A6=C2=BB constructors break on whitesp=
ace.
> >
> > So <a,b,c,d,e,f> will actually produce the following array:
> >
> >      ["a,b,c,d,e,f"]
> >
> > It's only one item.  If we placed space after the comma, that is, <a, b=
,
> c, d, e, f>, you'd get a six item list, but with the commas attached to a=
ll
> but the final:
> >
> >     ["a,", "b,", "c,", "d,", "e,", "f"]
> >
> > By replacing the commas with spaces, e.g., <a b c d e f>, you allow it
> to break into ["a", "b", "c", "d", "e", "f"]
> >
> > Mat=C3=A9u
> >
>
> Ya, I caught that booboo.  :'(
>
> Question still stands.  Why is the \n working as a CR/LF and
> being printed as a litteral?
>

--0000000000008c9e6a05b416a24f
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">=C2=A0 =C2=A0 &lt;a b c&gt;<div><br></div><div>is the same=
 as</div><div><br></div><div>=C2=A0 =C2=A0 Q :single :words &lt; a b c &gt;=
</div><div><br></div><div>Note that :single means it acts like single quote=
s.</div><div><br></div><div>Single quotes don&#39;t do anything to convert =
&#39;\n&#39; into anything other=C2=A0than a literal &#39;\n&#39;.</div><di=
v><br></div><div>If you want that to be converted to a linefeed you need to=
 use double quote semantics (or at least turn=C2=A0on :backslash).</div><di=
v><br>=C2=A0 =C2=A0 Q :double :words &lt; a\n b\n c &gt;<br></div><div><br>=
</div><div>Of course that also doesn&#39;t do what you want because a linef=
eed character is also whitespace, so it gets removed along with the rest of=
 the whitespace.</div><div><br></div><div>What you want to do use is :quote=
words and &quot;&quot;.</div><div><br></div><div>=C2=A0 =C2=A0 Q :quoteword=
s &lt; &quot;a\n&quot; &quot;b\n&quot; c &gt;<br></div><div><br></div><div>=
The short way to write that is</div><div><br></div><div>=C2=A0 =C2=A0 &lt;&=
lt; &quot;a\n&quot; &quot;b\n&quot; c &gt;&gt;</div><div><br></div><div>Alt=
hough if you are going to append a newline to every element I would conside=
r writing it this way:</div><div><br></div><div>=C2=A0 =C2=A0 &lt; a b c &g=
t; X~ &quot;\n&quot;</div><div><br></div><div>or</div><div><br></div><div>=
=C2=A0 =C2=A0 &lt; a b c &gt; =C2=BB~=C2=BB &quot;\n&quot;</div></div><br><=
div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Sat, Nov=
 14, 2020 at 1:21 PM ToddAndMargo via perl6-users &lt;<a href=3D"mailto:per=
l6-users@perl.org">perl6-users@perl.org</a>&gt; wrote:<br></div><blockquote=
 class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px so=
lid rgb(204,204,204);padding-left:1ex">=C2=A0&gt;&gt; On Nov 14, 2020, at 1=
4:12, ToddAndMargo via perl6-users <br>
&lt;<a href=3D"mailto:perl6-users@perl.org" target=3D"_blank">perl6-users@p=
erl.org</a>&gt; wrote:<br>
=C2=A0&gt;&gt;<br>
=C2=A0&gt;&gt; On 2020-11-14 11:08, Curt Tilmes wrote:<br>
=C2=A0&gt;&gt;&gt; On Sat, Nov 14, 2020 at 2:03 PM ToddAndMargo via perl6-u=
sers<br>
=C2=A0&gt;&gt;&gt; &lt;<a href=3D"mailto:perl6-users@perl.org" target=3D"_b=
lank">perl6-users@perl.org</a>&gt; wrote:<br>
=C2=A0&gt;&gt;&gt;&gt; Just out of curiosity, why is the \n printed out lit=
erally here?<br>
=C2=A0&gt;&gt;&gt;&gt; p6 &#39;my @x =3D &lt;&quot;aaa\n&quot;,&quot;bbb\n&=
quot;,&quot;ccc\n&quot;&gt;; for @x {print @_};&#39;<br>
=C2=A0&gt;&gt;&gt; Your &#39;word quoting&#39; &lt;&gt; is sort of like sin=
gle quotes -- it keeps the<br>
=C2=A0&gt;&gt;&gt; literal stuff.=C2=A0 You could<br>
=C2=A0&gt;&gt;&gt; use &lt;&lt;&gt;&gt; which is more like double quotes,<b=
r>
=C2=A0&gt;&gt;&gt; Curt<br>
=C2=A0&gt;&gt;<br>
=C2=A0&gt;&gt; or remove the commas.=C2=A0 I put everything in [0]<br>
=C2=A0&gt;&gt;<br>
=C2=A0&gt;&gt;<br>
=C2=A0&gt;&gt; $ p6 &#39;my @x =3D &lt;aaa\n bbb\n ccc\n&gt;; for @x {print=
 &quot;$_\n&quot;;}&#39;<br>
=C2=A0&gt;&gt; aaa\n<br>
=C2=A0&gt;&gt; bbb\n<br>
=C2=A0&gt;&gt; ccc\n<br>
=C2=A0&gt;&gt;<br>
=C2=A0&gt;&gt; $ p6 &#39;my @x =3D &lt;&lt;aaa\n bbb\n ccc\n&gt;&gt;; for @=
x {print &quot;$_\n&quot;;}&#39;<br>
=C2=A0&gt;&gt; aaa<br>
=C2=A0&gt;&gt; bbb<br>
=C2=A0&gt;&gt; ccc<br>
=C2=A0&gt;&gt;<br>
=C2=A0&gt;&gt; $ p6 &#39;my @x =3D &lt;&lt;aaa\n bbb\n ccc\n&gt;&gt;; for @=
x {print &quot;$_&quot;;}&#39;<br>
=C2=A0&gt;&gt; aaabbbccc<br>
=C2=A0&gt;&gt;<br>
=C2=A0&gt;&gt; What am I missing?<br>
=C2=A0&gt;&gt;<br>
=C2=A0&gt;&gt; -T<br>
<br>
<br>
On 2020-11-14 11:18, Matthew Stuckwisch wrote:<br>
&gt; The &lt;=E2=80=A6&gt; and =C2=AB=E2=80=A6=C2=BB constructors break on =
whitespace.<br>
&gt; <br>
&gt; So &lt;a,b,c,d,e,f&gt; will actually produce the following array:<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0 [&quot;a,b,c,d,e,f&quot;]<br>
&gt; <br>
&gt; It&#39;s only one item.=C2=A0 If we placed space after the comma, that=
 is, &lt;a, b, c, d, e, f&gt;, you&#39;d get a six item list, but with the =
commas attached to all but the final:<br>
&gt; <br>
&gt;=C2=A0 =C2=A0 =C2=A0[&quot;a,&quot;, &quot;b,&quot;, &quot;c,&quot;, &q=
uot;d,&quot;, &quot;e,&quot;, &quot;f&quot;]<br>
&gt; <br>
&gt; By replacing the commas with spaces, e.g., &lt;a b c d e f&gt;, you al=
low it to break into [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&=
quot;, &quot;e&quot;, &quot;f&quot;]<br>
&gt; <br>
&gt; Mat=C3=A9u<br>
&gt; <br>
<br>
Ya, I caught that booboo.=C2=A0 :&#39;(<br>
<br>
Question still stands.=C2=A0 Why is the \n working as a CR/LF and<br>
being printed as a litteral?<br>
</blockquote></div>

--0000000000008c9e6a05b416a24f--
0
b2gills
11/14/2020 8:03:01 PM
On 2020-11-14 12:03, Brad Gilbert wrote:
>  =C2=A0 <a b c>

I pretty quickly caught my booboo after I pressed send.
A little eggs on the face.

But my question still holds.  Why is the \n inside
the cell printed literally?
0
perl6
11/14/2020 8:06:30 PM

> On Nov 14, 2020, at 2:06 PM, ToddAndMargo via perl6-users =
<perl6-users@perl.org> wrote:

=E2=80=94snip=E2=80=94

> But my question still holds. =20
> Why is the \n inside the cell printed literally?

The two characters, backslash and `n`, are output literally,
because you have *input* them literally.

In single quotes, the backslash does not combine with the `n` at all. =
They both remain separate characters.
Single angle brackets follow single quoting rules, and only then split =
on whitespace.

When you said:
	my @x =3D <"aaa\n","bbb\n","ccc\n">;
, you populated @x with only one element.

That element is a single 23-character string, with no whitespace in it.


Example code, for exploration:
    my @z =3D
        "\n"  ,   # 1 char : newline
        '\n'  ,   # 2 chars: backslash, 'n'
        <\n>  ,   # 2 chars: backslash, 'n'
       <<\n>> ,   # Empty list: because newline is whitespace, so it =
vanishes

        "a \n b"  ,   # 5 chars: 'a', newline, 'b'
        'a \n b'  ,   # 6 chars: 'a', backslash, 'n', 'b'
        <a \n b>  ,   # List of 3 elements, of lengths 1,2,1: 'a', '\n', =
=E2=80=98b=E2=80=99  The only whitespaces are the two separate space =
characters (one on the left of the backslash character, and one to the =
right of the =E2=80=99n=E2=80=99 character), hence the word-quoting =
creates 3 elements.
       <<a \n b>> ,   # List of 2 elements, each is 1 char: 'a', 'b=E2=80=99=
;  The =E2=80=9Cspace newline space=E2=80=9D is all a big chunk of =
whitespace, and the word-quoting effect just uses it to separate =E2=80=98=
a=E2=80=99 and =E2=80=98b'
    ;
    say $_ ~~ Str ?? .chars !! .list=C2=BB.chars for @z;=20
    say '';
    dd $_ for @z;

Output:
    1
    2
    2
    ()
    5
    6
    (1 2 1)
    (1 1)

    Str @z =3D "\n"
    Str @z =3D "\\n"
    Str @z =3D "\\n"
    List @z =3D $( )
    Str @z =3D "a \n b"
    Str @z =3D "a \\n b"
    List @z =3D $("a", "\\n", "b")
    List @z =3D $("a", "b")

=E2=80=94=20
Hope this helps,
Bruce Gray (Util of PerlMonks)
0
robertbrucegray3
11/15/2020 2:03:31 AM
On 2020-11-14 18:03, Bruce Gray wrote:
>=20
>=20
>> On Nov 14, 2020, at 2:06 PM, ToddAndMargo via perl6-users <perl6-users=
@perl.org> wrote:
>=20
> =E2=80=94snip=E2=80=94
>=20
>> But my question still holds.
>> Why is the \n inside the cell printed literally?
>=20
> The two characters, backslash and `n`, are output literally,
> because you have *input* them literally.
>=20
> In single quotes, the backslash does not combine with the `n` at all. T=
hey both remain separate characters.
> Single angle brackets follow single quoting rules, and only then split =
on whitespace.
>=20
> When you said:
> 	my @x =3D <"aaa\n","bbb\n","ccc\n">;
> , you populated @x with only one element.
>=20
> That element is a single 23-character string, with no whitespace in it.=

>=20
>=20
> Example code, for exploration:
>      my @z =3D
>          "\n"  ,   # 1 char : newline
>          '\n'  ,   # 2 chars: backslash, 'n'
>          <\n>  ,   # 2 chars: backslash, 'n'
>         <<\n>> ,   # Empty list: because newline is whitespace, so it v=
anishes
>=20
>          "a \n b"  ,   # 5 chars: 'a', newline, 'b'
>          'a \n b'  ,   # 6 chars: 'a', backslash, 'n', 'b'
>          <a \n b>  ,   # List of 3 elements, of lengths 1,2,1: 'a', '\n=
', =E2=80=98b=E2=80=99  The only whitespaces are the two separate space c=
haracters (one on the left of the backslash character, and one to the rig=
ht of the =E2=80=99n=E2=80=99 character), hence the word-quoting creates =
3 elements.
>         <<a \n b>> ,   # List of 2 elements, each is 1 char: 'a', 'b=E2=
=80=99;  The =E2=80=9Cspace newline space=E2=80=9D is all a big chunk of =
whitespace, and the word-quoting effect just uses it to separate =E2=80=98=
a=E2=80=99 and =E2=80=98b'
>      ;
>      say $_ ~~ Str ?? .chars !! .list=C2=BB.chars for @z;
>      say '';
>      dd $_ for @z;
>=20
> Output:
>      1
>      2
>      2
>      ()
>      5
>      6
>      (1 2 1)
>      (1 1)
>=20
>      Str @z =3D "\n"
>      Str @z =3D "\\n"
>      Str @z =3D "\\n"
>      List @z =3D $( )
>      Str @z =3D "a \n b"
>      Str @z =3D "a \\n b"
>      List @z =3D $("a", "\\n", "b")
>      List @z =3D $("a", "b")
>=20
> =E2=80=94
> Hope this helps,
> Bruce Gray (Util of PerlMonks)
>=20

Hi Bruce,

Yes it was helpful:

$ p6 'my @x =3D "aaa\n", "bbb\n", "ccc\n"; dd @x; say @x; for @x {print=20
"$_";}'

Array @x =3D ["aaa\n", "bbb\n", "ccc\n"]

[aaa
  bbb
  ccc
]

aaa
bbb
ccc

Joy!  Thank you!

-T
0
perl6
11/15/2020 3:29:28 AM
Reply: