On 2020-11-14 18:03, Bruce Gray wrote:


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

—snip—

But my question still holds.
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 = <"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 =
         "\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', ‘b’  
The only whitespaces are the two separate space characters (one on the left of the 
backslash character, and one to the right of the ’n’ character), hence the 
word-quoting creates 3 elements.
        <<a \n b>> ,   # List of 2 elements, each is 1 char: 'a', 'b’;  The 
“space newline space” is all a big chunk of whitespace, and the word-quoting effect just 
uses it to separate ‘a’ and ‘b'
     ;
     say $_ ~~ Str ?? .chars !! .list».chars for @z;
     say '';
     dd $_ for @z;

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

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

—
Hope this helps,
Bruce Gray (Util of PerlMonks)


Hi Bruce,

Yes it was helpful:

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

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

[aaa
 bbb
 ccc
]

aaa
bbb
ccc

Joy!  Thank you!

-T

Reply via email to