spurt and array question

Hi All,

I am writing out an array of text lines to a file.
I just can't help but thinking I am doing it the
hard way.

    unlink( $Leafpadrc );
    for @LeafpadrcNew -> $Line  { spurt( $Leafpadrc, $Line ~ "\n", 
:append ); }

If I spurt the array, it converts the array into a
single text line.

The test file looks like this:

         ./.config/leafpad/leafpadrc
         $ cat leafpadrc
         0.8.18.1
         500
         190
         Monospace 12
         1
         1
         0

Your thoughts?

-T

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

14 Replies
26 Views

Similar Articles

[PageSpeed] 36

--0000000000009cf53705b40f44c1
Content-Type: text/plain; charset="UTF-8"

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

> Hi All,
>
> I am writing out an array of text lines to a file.
> I just can't help but thinking I am doing it the
> hard way.
>
>     unlink( $Leafpadrc );
>     for @LeafpadrcNew -> $Line  { spurt( $Leafpadrc, $Line ~ "\n",
> :append ); }
>

Unless I misunderstand, why doesn't this work:

    my $fh = open $Leafpadrc, :w;
    $fh.say($_) for @Leafpadrc;

-Tom

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

<div>On Sat, Nov 14, 2020 at 01:59 ToddAndMargo via perl6-users &lt;<a href=
=3D"mailto:perl6-users@perl.org">perl6-users@perl.org</a>&gt; wrote:<br></d=
iv><div><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" style=
=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi All,<=
br>
<br>
I am writing out an array of text lines to a file.<br>
I just can&#39;t help but thinking I am doing it the<br>
hard way.<br>
<br>
=C2=A0 =C2=A0 unlink( $Leafpadrc );<br>
=C2=A0 =C2=A0 for @LeafpadrcNew -&gt; $Line=C2=A0 { spurt( $Leafpadrc, $Lin=
e ~ &quot;\n&quot;, <br>
:append ); }<br>
</blockquote><div dir=3D"auto"><br></div><div dir=3D"auto">Unless I misunde=
rstand, why doesn&#39;t this work:</div><div dir=3D"auto">=C2=A0 =C2=A0=C2=
=A0</div><div dir=3D"auto">=C2=A0 =C2=A0 my $fh =3D open $Leafpadrc, :w;</d=
iv><div dir=3D"auto">=C2=A0 =C2=A0 $fh.say($_) for @Leafpadrc;</div><div di=
r=3D"auto">=C2=A0 =C2=A0</div><div dir=3D"auto">-Tom</div></div></div>

--0000000000009cf53705b40f44c1--
0
tom
11/14/2020 11:15:41 AM
On 2020-11-13 ToddAndMargo via perl6-users <perl6-users@perl.org> wrote:
> Hi All,
> 
> I am writing out an array of text lines to a file.
> I just can't help but thinking I am doing it the
> hard way.
> 
>     unlink( $Leafpadrc );
>     for @LeafpadrcNew -> $Line  { spurt( $Leafpadrc, $Line ~ "\n", 
> :append ); }
> 
> If I spurt the array, it converts the array into a
> single text line.

Some alternatives:

  $Leafpadrc.spurt(@LeafpadrcNew.join($Leafpadrc.nl-out));

  $Leafpadrc.put($_) for @LeafpadrcNew;

-- 
	Dakkar - <Mobilis in mobile>
	GPG public key fingerprint = A071 E618 DD2C 5901 9574
	                             6FE2 40EA 9883 7519 3F88
	                    key id = 0x75193F88
0
dakkar
11/14/2020 11:59:48 AM
--00000000000092c65a05b411915c
Content-Type: text/plain; charset="UTF-8"

The purpose of `spurt` is to:
1. open a NEW file to write to
2. print a single string
3. close the file

If you are calling `spurt` more than once on a given file, you are doing it
wrong.
If you give `spurt` an array, you are probably doing it wrong; unless you
want the array turned into a single string first.

`spurt` is the dual of `slurp`.

The purpose of `slurp` is to:
1. open an existing file to read from
2. read the whole file into a single string
3. close the file

That is they are only short-cuts for a simple combination of operations.

If you are opening a file for only the express purpose of reading ALL of
its contents into a SINGLE STRING, use `slurp`.
If you are opening a file for only the express purpose of writing ALL of
its contents from a SINGLE STRING, use `spurt`.

If you are doing anything else, use something else.

---

Assuming you want to loop over a bunch of strings to print to a file, use
`open` and `print`/`put`/`say`.
This is also faster than calling `spurt` more than once because you only
open and close the file once.

If you want there to be only one call, turn your array into the appropriate
single string first.

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

> Hi All,
>
> I am writing out an array of text lines to a file.
> I just can't help but thinking I am doing it the
> hard way.
>
>     unlink( $Leafpadrc );
>     for @LeafpadrcNew -> $Line  { spurt( $Leafpadrc, $Line ~ "\n",
> :append ); }
>
> If I spurt the array, it converts the array into a
> single text line.
>
> The test file looks like this:
>
>          ./.config/leafpad/leafpadrc
>          $ cat leafpadrc
>          0.8.18.1
>          500
>          190
>          Monospace 12
>          1
>          1
>          0
>
> Your thoughts?
>
> -T
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Computers are like air conditioners.
> They malfunction when you open windows
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>

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

<div dir=3D"ltr">The purpose of `spurt` is to:<div>1. open a NEW file to wr=
ite to</div><div>2. print a single string</div><div>3. close the file</div>=
<div><br></div><div>If you are calling `spurt` more than once on a given fi=
le, you are doing it wrong.</div><div>If you give `spurt` an array, you are=
 probably doing it wrong; unless=C2=A0you want the array turned into a sing=
le string first.</div><div><br></div><div>`spurt` is the dual of `slurp`.</=
div><div><br></div><div>The purpose of `slurp` is to:</div><div>1. open an =
existing file to read from</div><div>2. read the whole file into a single s=
tring</div><div>3. close the file</div><div><br></div><div>That is they are=
 only short-cuts for a simple combination of operations.</div><div><br></di=
v><div>If you are opening a file for only the express purpose of reading AL=
L of its contents into a SINGLE STRING, use `slurp`.</div><div>If you are o=
pening a file for only the express purpose of writing ALL of its contents f=
rom a SINGLE STRING, use `spurt`.</div><div><br></div><div>If you are doing=
 anything else, use something else.</div><div><br></div><div>---</div><div>=
<br></div><div>Assuming=C2=A0you want to loop over a bunch of strings to pr=
int to a file, use `open` and `print`/`put`/`say`.</div><div>This is also f=
aster than calling `spurt` more than once because you only open and close t=
he file once.</div><div><br></div><div>If you want there to be only one cal=
l, turn your array into the appropriate single string first.</div></div><br=
><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Sat, N=
ov 14, 2020 at 1:59 AM ToddAndMargo via perl6-users &lt;<a href=3D"mailto:p=
erl6-users@perl.org">perl6-users@perl.org</a>&gt; wrote:<br></div><blockquo=
te class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px =
solid rgb(204,204,204);padding-left:1ex">Hi All,<br>
<br>
I am writing out an array of text lines to a file.<br>
I just can&#39;t help but thinking I am doing it the<br>
hard way.<br>
<br>
=C2=A0 =C2=A0 unlink( $Leafpadrc );<br>
=C2=A0 =C2=A0 for @LeafpadrcNew -&gt; $Line=C2=A0 { spurt( $Leafpadrc, $Lin=
e ~ &quot;\n&quot;, <br>
:append ); }<br>
<br>
If I spurt the array, it converts the array into a<br>
single text line.<br>
<br>
The test file looks like this:<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0./.config/leafpad/leafpadrc<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0$ cat leafpadrc<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A00.8.18.1<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0500<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0190<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Monospace 12<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A01<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A01<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A00<br>
<br>
Your thoughts?<br>
<br>
-T<br>
<br>
-- <br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
Computers are like air conditioners.<br>
They malfunction when you open windows<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
</blockquote></div>

--00000000000092c65a05b411915c--
0
b2gills
11/14/2020 2:00:22 PM
On 2020-11-14 06:00, Brad Gilbert wrote:
> The purpose of `spurt` is to:
> 1. open a NEW file to write to
> 2. print a single string
> 3. close the file
>=20
> If you are calling `spurt` more than once on a given file, you are doin=
g=20
> it wrong.

You are forgetting that spurt comes with an `:append` option.

> If you give `spurt` an array, you are probably doing it wrong
> unless=C2=A0you want the array turned into a single string first.

Ya, doing things the hard way.
0
perl6
11/14/2020 7:07:20 PM
--000000000000a7c05b05b4161126
Content-Type: text/plain; charset="UTF-8"

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

> On 2020-11-14 06:00, Brad Gilbert wrote:
> > The purpose of `spurt` is to:
> > 1. open a NEW file to write to
> > 2. print a single string
> > 3. close the file
> >
> > If you are calling `spurt` more than once on a given file, you are doing
> > it wrong.
>
> You are forgetting that spurt comes with an `:append` option.
>

Maybe this is what you want:

my @a = 1,2,3;
spurt('test', @a.join("\n") ~ "\n");  # join doesn't add the last "\n"

Or the equivalent

'test'.IO.spurt: @a.join("\n") ~ "\n";

-- 
Fernando Santagata

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

<div dir=3D"ltr"><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail=
_attr">On Sat, Nov 14, 2020 at 8:07 PM ToddAndMargo via perl6-users &lt;<a =
href=3D"mailto:perl6-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 solid rgb(204,204,204);padding-left:1ex">On 2020-11-14 06:0=
0, Brad Gilbert wrote:<br>
&gt; The purpose of `spurt` is to:<br>
&gt; 1. open a NEW file to write to<br>
&gt; 2. print a single string<br>
&gt; 3. close the file<br>
&gt; <br>
&gt; If you are calling `spurt` more than once on a given file, you are doi=
ng <br>
&gt; it wrong.<br>
<br>
You are forgetting that spurt comes with an `:append` option.<br></blockquo=
te><div><br></div><div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_defau=
lt">Maybe this is what you want:</div><div style=3D"color:rgb(0,0,0)" class=
=3D"gmail_default"><br></div><div style=3D"color:rgb(0,0,0)" class=3D"gmail=
_default">my @a =3D 1,2,3;</div><div style=3D"color:rgb(0,0,0)" class=3D"gm=
ail_default">spurt(&#39;test&#39;, @a.join(&quot;\n&quot;) ~ &quot;\n&quot;=
);=C2=A0 # join doesn&#39;t add the last &quot;\n&quot;<br></div></div><div=
><br></div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default">Or the e=
quivalent</div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default"><br>=
</div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default">&#39;test&#39=
;.IO.spurt: @a.join(&quot;\n&quot;) ~ &quot;\n&quot;;</div><div><br clear=
=3D"all"></div></div>-- <br><div dir=3D"ltr" class=3D"gmail_signature">Fern=
ando Santagata</div></div>

--000000000000a7c05b05b4161126--
0
nando
11/14/2020 7:22:30 PM
On 2020-11-14 03:59, Gianni Ceccarelli wrote:
> $Leafpadrc.put($_) for @LeafpadrcNew;

Cannot resolve caller print(Str:D: BOOTStr); none of these signatures match:
     (Mu: *%_)
   in sub RunReport at ./XferParts.pl6 line 229

229: $Leafpadrc.put($_) for @LeafpadrcNew;

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Computers are like air conditioners.
They malfunction when you open windows
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
perl6
11/14/2020 7:41:12 PM
--0000000000001825f105b41679bf
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Actually no I'm not =E2=80=9Cforgetting that spurt comes with an `:append` =
option=E2=80=9D.
That is a slightly different use case.
It is where you are appending to an existing file once, and then never
touching it again.

(Or maybe you might be touching it again in a few hours.)

---

Given that this is what you wrote:

    unlink( $Leafpadrc );
    for @LeafpadrcNew -> $Line  { spurt( $Leafpadrc, $Line ~ "\n", :append
); }

I want to know how this is the hard way:

    given $Leafpadrc.IO.open(:w) {
        for @LeafpadrcNew -> $Line  { .put: $Line }
        .close;
    }

or

    given $Leafpadrc.IO.open(:w) -> $*OUT {
        for @LeafpadrcNew -> $Line  { put $Line }
        $*OUT.close;
    }

or

    given $Leafpadrc.IO.open(:w) -> $*OUT {
        .put for @LeafpadrcNew;
        $*OUT.close;
    }

or

    given $Leafpadrc.IO.open(:w, :!out-buffer) -> $*OUT {
        .put for @LeafpadrcNew;
    }



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

> On 2020-11-14 06:00, Brad Gilbert wrote:
> > The purpose of `spurt` is to:
> > 1. open a NEW file to write to
> > 2. print a single string
> > 3. close the file
> >
> > If you are calling `spurt` more than once on a given file, you are doin=
g
> > it wrong.
>
> You are forgetting that spurt comes with an `:append` option.
>
> > If you give `spurt` an array, you are probably doing it wrong
> > unless you want the array turned into a single string first.
>
> Ya, doing things the hard way.
>

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

<div dir=3D"ltr">Actually no I&#39;m not =E2=80=9Cforgetting that spurt com=
es with an `:append` option=E2=80=9D.<div>That is a slightly different use =
case.<br></div><div>It is where you are appending to an existing file once,=
 and then never touching it again.<br></div><div><br></div><div>(Or maybe y=
ou might be=C2=A0touching it again in a few hours.)</div><div><br></div><di=
v>---</div><div><br></div><div>Given that this is what you wrote:</div><div=
><br></div><div>=C2=A0 =C2=A0 unlink( $Leafpadrc );<br>=C2=A0 =C2=A0 for @L=
eafpadrcNew -&gt; $Line =C2=A0{ spurt( $Leafpadrc, $Line ~ &quot;\n&quot;, =
:append ); }<br></div><div><div><br></div><div>I want to know how this is t=
he hard way:</div><br>=C2=A0 =C2=A0 given=C2=A0$Leafpadrc.IO.open(:w) {<br>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 for=C2=A0@LeafpadrcNew -&gt; $Line =C2=A0{ .put=
: $Line }<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 .close;<br>=C2=A0 =C2=A0 }</div><d=
iv><br></div><div>or</div><div><br></div><div>=C2=A0 =C2=A0 given=C2=A0$Lea=
fpadrc.IO.open(:w) -&gt; $*OUT {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 for=
=C2=A0@LeafpadrcNew -&gt; $Line =C2=A0{ put $Line }</div><div>=C2=A0 =C2=A0=
 =C2=A0 =C2=A0 $*OUT.close;</div><div>=C2=A0 =C2=A0 }</div><div><br></div><=
div>or</div><div><br>=C2=A0 =C2=A0 given=C2=A0$Leafpadrc.IO.open(:w) -&gt; =
$*OUT {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 .put for=C2=A0@LeafpadrcNew;</div><d=
iv>=C2=A0 =C2=A0 =C2=A0 =C2=A0 $*OUT.close;<div></div>=C2=A0 =C2=A0 }</div>=
<div><br></div><div>or</div><div><br>=C2=A0 =C2=A0 given=C2=A0$Leafpadrc.IO=
..open(:w, :!out-buffer) -&gt; $*OUT {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 .put f=
or=C2=A0@LeafpadrcNew;<br>=C2=A0 =C2=A0 }<br></div><div><br></div><div><br>=
</div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_=
attr">On Sat, Nov 14, 2020 at 1:07 PM ToddAndMargo via perl6-users &lt;<a h=
ref=3D"mailto:perl6-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;b=
order-left:1px solid rgb(204,204,204);padding-left:1ex">On 2020-11-14 06:00=
, Brad Gilbert wrote:<br>
&gt; The purpose of `spurt` is to:<br>
&gt; 1. open a NEW file to write to<br>
&gt; 2. print a single string<br>
&gt; 3. close the file<br>
&gt; <br>
&gt; If you are calling `spurt` more than once on a given file, you are doi=
ng <br>
&gt; it wrong.<br>
<br>
You are forgetting that spurt comes with an `:append` option.<br>
<br>
&gt; If you give `spurt` an array, you are probably doing it wrong<br>
&gt; unless=C2=A0you want the array turned into a single string first.<br>
<br>
Ya, doing things the hard way.<br>
</blockquote></div>

--0000000000001825f105b41679bf--
0
b2gills
11/14/2020 7:51:25 PM
On 2020-11-14 03:15, Tom Browder wrote:
> On Sat, Nov 14, 2020 at 01:59 ToddAndMargo via perl6-users=20
> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>=20
>     Hi All,
>=20
>     I am writing out an array of text lines to a file.
>     I just can't help but thinking I am doing it the
>     hard way.
>=20
>      =C2=A0 =C2=A0 unlink( $Leafpadrc );
>      =C2=A0 =C2=A0 for @LeafpadrcNew -> $Line=C2=A0 { spurt( $Leafpadrc=
, $Line ~ "\n",
>     :append ); }
>=20
>=20
> Unless I misunderstand, why doesn't this work:
>  =C2=A0 =C2=A0 my $fh =3D open $Leafpadrc, :w;
>  =C2=A0 =C2=A0 $fh.say($_) for @Leafpadrc;
> -Tom



    unlink( $Leafpadrc );
    $Leafpadrc.IO.open( :w );

Neither of these two actually updates the file.

    for @LeafpadrcNew -> $Line  { put( $Leafpadrc, $Line ); }
    for @LeafpadrcNew -> $Line  { $Leafpadrc.put( $Line ); }
0
perl6
11/14/2020 7:59:20 PM
On 2020-11-14 11:22, Fernando Santagata wrote:
> On Sat, Nov 14, 2020 at 8:07 PM ToddAndMargo via perl6-users=20
> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>=20
>     On 2020-11-14 06:00, Brad Gilbert wrote:
>      > The purpose of `spurt` is to:
>      > 1. open a NEW file to write to
>      > 2. print a single string
>      > 3. close the file
>      >
>      > If you are calling `spurt` more than once on a given file, you
>     are doing
>      > it wrong.
>=20
>     You are forgetting that spurt comes with an `:append` option.
>=20
>=20
> Maybe this is what you want:
>=20
> my @a =3D 1,2,3;
> spurt('test', @a.join("\n") ~ "\n");=C2=A0 # join doesn't add the last =
"\n"
>=20
> Or the equivalent
>=20
> 'test'.IO.spurt: @a.join("\n") ~ "\n";
>=20
> --=20
> Fernando Santagata

That is the way around the issue.

But my question is why can I not put the \n in the variable?
0
perl6
11/14/2020 8:01:57 PM
>> On Sat, Nov 14, 2020 at 1:07 PM ToddAndMargo via perl6-users=20
>> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
>>=20
>>     On 2020-11-14 06:00, Brad Gilbert wrote:
>>      > The purpose of `spurt` is to:
>>      > 1. open a NEW file to write to
>>      > 2. print a single string
>>      > 3. close the file
>>      >
>>      > If you are calling `spurt` more than once on a given file, you
>>     are doing
>>      > it wrong.
>>=20
>>     You are forgetting that spurt comes with an `:append` option.
>>=20
>>      > If you give `spurt` an array, you are probably doing it wrong
>>      > unless you want the array turned into a single string first.
>>=20
>>     Ya, doing things the hard way.
>>=20


On 2020-11-14 11:51, Brad Gilbert wrote:
> Actually no I'm not =E2=80=9Cforgetting that spurt comes with an `:appe=
nd` option=E2=80=9D.
> That is a slightly different use case.
> It is where you are appending to an existing file once, and then never =

> touching it again.
>=20
> (Or maybe you might be=C2=A0touching it again in a few hours.)
>=20
> ---
>=20
> Given that this is what you wrote:
>=20
>  =C2=A0 =C2=A0 unlink( $Leafpadrc );
>  =C2=A0 =C2=A0 for @LeafpadrcNew -> $Line =C2=A0{ spurt( $Leafpadrc, $L=
ine ~ "\n",=20
> :append ); }
>=20
> I want to know how this is the hard way:
>=20
>  =C2=A0 =C2=A0 given=C2=A0$Leafpadrc.IO.open(:w) {
>  =C2=A0 =C2=A0 =C2=A0 =C2=A0 for=C2=A0@LeafpadrcNew -> $Line =C2=A0{ .p=
ut: $Line }
>  =C2=A0 =C2=A0 =C2=A0 =C2=A0 .close;
>  =C2=A0 =C2=A0 }
>=20
> or
>=20
>  =C2=A0 =C2=A0 given=C2=A0$Leafpadrc.IO.open(:w) -> $*OUT {
>  =C2=A0 =C2=A0 =C2=A0 =C2=A0 for=C2=A0@LeafpadrcNew -> $Line =C2=A0{ pu=
t $Line }
>  =C2=A0 =C2=A0 =C2=A0 =C2=A0 $*OUT.close;
>  =C2=A0 =C2=A0 }
>=20
> or
>=20
>  =C2=A0 =C2=A0 given=C2=A0$Leafpadrc.IO.open(:w) -> $*OUT {
>  =C2=A0 =C2=A0 =C2=A0 =C2=A0 .put for=C2=A0@LeafpadrcNew;
>  =C2=A0 =C2=A0 =C2=A0 =C2=A0 $*OUT.close;
>  =C2=A0 =C2=A0 }
>=20
> or
>=20
>  =C2=A0 =C2=A0 given=C2=A0$Leafpadrc.IO.open(:w, :!out-buffer) -> $*OUT=
 {
>  =C2=A0 =C2=A0 =C2=A0 =C2=A0 .put for=C2=A0@LeafpadrcNew;
>  =C2=A0 =C2=A0 }


I was saying I was doing it the hard way, not you.

Wonderful examples.  Thank you!
0
perl6
11/14/2020 8:03:38 PM
--000000000000ab9e9505b417fa1c
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

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

> > Maybe this is what you want:
> >
> > my @a =3D 1,2,3;
> > spurt('test', @a.join("\n") ~ "\n");  # join doesn't add the last "\n"
> >
> > Or the equivalent
> >
> > 'test'.IO.spurt: @a.join("\n") ~ "\n";
>
> That is the way around the issue.
>
> But my question is why can I not put the \n in the variable?
>

What do you mean by putting the \n in the variable?
Is it anything like this? [=C2=B9]

my @a =3D "1\n", "2\n", "3\n";
'test'.IO.spurt(@a);

or this?

my @a =3D <a b c>;
'test'.IO.spurt(@a =C2=BB~=C2=BB "\n");


[=C2=B9] Mind that the array is first converted into a string and its eleme=
nts
are joined together with an interleaving space

--=20
Fernando Santagata

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

<div dir=3D"ltr"><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail=
_attr">On Sat, Nov 14, 2020 at 9:02 PM ToddAndMargo via perl6-users &lt;<a =
href=3D"mailto:perl6-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 solid rgb(204,204,204);padding-left:1ex">&gt; Maybe this is=
 what you want:<br>
&gt; <br>
&gt; my @a =3D 1,2,3;<br>
&gt; spurt(&#39;test&#39;, @a.join(&quot;\n&quot;) ~ &quot;\n&quot;);=C2=A0=
 # join doesn&#39;t add the last &quot;\n&quot;<br>
&gt; <br>
&gt; Or the equivalent<br>
&gt; <br>
&gt; &#39;test&#39;.IO.spurt: @a.join(&quot;\n&quot;) ~ &quot;\n&quot;;<br>=
<br>
That is the way around the issue.<br>
<br>
But my question is why can I not put the \n in the variable?<br>
</blockquote></div><br clear=3D"all"><div><div style=3D"color:rgb(0,0,0)" c=
lass=3D"gmail_default">What do you mean by putting the \n in the variable?<=
/div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default">Is it anything=
 like this? [=C2=B9]<br></div><div style=3D"color:rgb(0,0,0)" class=3D"gmai=
l_default"><br></div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default=
">my @a =3D &quot;1\n&quot;, &quot;2\n&quot;, &quot;3\n&quot;;</div><div st=
yle=3D"color:rgb(0,0,0)" class=3D"gmail_default">&#39;test&#39;.IO.spurt(@a=
);</div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default"><br></div><=
div style=3D"color:rgb(0,0,0)" class=3D"gmail_default">or this?</div><div s=
tyle=3D"color:rgb(0,0,0)" class=3D"gmail_default"><br></div><div style=3D"c=
olor:rgb(0,0,0)" class=3D"gmail_default">my @a =3D &lt;a b c&gt;;</div><div=
 style=3D"color:rgb(0,0,0)" class=3D"gmail_default">&#39;test&#39;.IO.spurt=
(@a =C2=BB~=C2=BB &quot;\n&quot;);</div></div><div><br></div><div><br></div=
><div></div><div><div style=3D"color:rgb(0,0,0)" class=3D"gmail_default">[=
=C2=B9] Mind that the array is first converted into a string and its elemen=
ts are joined together with an interleaving space<br></div></div><div><br><=
/div>-- <br><div dir=3D"ltr" class=3D"gmail_signature">Fernando Santagata</=
div></div>

--000000000000ab9e9505b417fa1c--
0
nando
11/14/2020 9:39:15 PM
On 2020-11-14 13:39, Fernando Santagata wrote:
> What do you mean by putting the \n in the variable?

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

Why are the \n's not being resolved in the above?

Why do I have to add an \n to the print line?

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

Oh I see, because they are not actually in the cell:

$ p6 'my @x = <<aaa\n bbb\n ccc\n>>; dd @x'
Array @x = ["aaa", "bbb", "ccc"]
0
perl6
11/14/2020 11:25:18 PM
--000000000000882a9c05b4209bfc
Content-Type: text/plain; charset="UTF-8"

Oh, now I see: you were asking that question in another thread.
<<>> is equivalent to qq:ww:v as mentioned here:

https://docs.raku.org/syntax/%3C%3C%20%3E%3E#index-entry-%3Aval_%28quoting_adverb%29

and as stated here:

https://docs.raku.org/language/quoting

the adverb :ww splits the string into words using whitespace characters as
separators.
Now, being "\n" a whitespace character, your string <<aaa\n bbb\n ccc\n>>
was split in three parts ("aaa", "bbb", "ccc") with no whitespace
characters in them.

On Sun, Nov 15, 2020 at 12:25 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 2020-11-14 13:39, Fernando Santagata wrote:
> > What do you mean by putting the \n in the variable?
>
> $ p6 'my @x = <<aaa\n bbb\n ccc\n>>; for @x {"$_".print};'
> aaabbbccc
>
> Why are the \n's not being resolved in the above?
>
> Why do I have to add an \n to the print line?
>
> $ p6 'my @x = <<aaa\n bbb\n ccc\n>>; for @x {"$_\n".print};'
> aaa
> bbb
> ccc
>
> Oh I see, because they are not actually in the cell:
>
> $ p6 'my @x = <<aaa\n bbb\n ccc\n>>; dd @x'
> Array @x = ["aaa", "bbb", "ccc"]
>


-- 
Fernando Santagata

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

<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"color:#000000">Oh, n=
ow I see: you were asking that question in another thread.</div><div class=
=3D"gmail_default" style=3D"color:#000000">&lt;&lt;&gt;&gt; is equivalent t=
o qq:ww:v as mentioned here:</div><div class=3D"gmail_default" style=3D"col=
or:#000000"><br></div><div class=3D"gmail_default" style=3D"color:#000000">=
<a href=3D"https://docs.raku.org/syntax/%3C%3C%20%3E%3E#index-entry-%3Aval_=
%28quoting_adverb%29">https://docs.raku.org/syntax/%3C%3C%20%3E%3E#index-en=
try-%3Aval_%28quoting_adverb%29</a></div><div class=3D"gmail_default" style=
=3D"color:#000000"><br></div><div class=3D"gmail_default" style=3D"color:#0=
00000">and as stated here:<br></div><div class=3D"gmail_default" style=3D"c=
olor:#000000"><br></div><div class=3D"gmail_default" style=3D"color:#000000=
"><a href=3D"https://docs.raku.org/language/quoting">https://docs.raku.org/=
language/quoting</a></div><div class=3D"gmail_default" style=3D"color:#0000=
00"><br></div><div class=3D"gmail_default" style=3D"color:#000000">the adve=
rb :ww splits the string into words using whitespace characters as separato=
rs.</div><div class=3D"gmail_default" style=3D"color:#000000">Now, being &q=
uot;\n&quot; a whitespace character, your string &lt;&lt;aaa\n bbb\n ccc\n&=
gt;&gt; was split in three parts (&quot;aaa&quot;, &quot;bbb&quot;, &quot;c=
cc&quot;) with no whitespace characters in them.<br></div></div><br><div cl=
ass=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Sun, Nov 15, 2=
020 at 12:25 AM ToddAndMargo via perl6-users &lt;<a href=3D"mailto:perl6-us=
ers@perl.org">perl6-users@perl.org</a>&gt; wrote:<br></div><blockquote clas=
s=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid r=
gb(204,204,204);padding-left:1ex">On 2020-11-14 13:39, Fernando Santagata w=
rote:<br>
&gt; What do you mean by putting the \n in the variable?<br>
<br>
$ p6 &#39;my @x =3D &lt;&lt;aaa\n bbb\n ccc\n&gt;&gt;; for @x {&quot;$_&quo=
t;.print};&#39;<br>
aaabbbccc<br>
<br>
Why are the \n&#39;s not being resolved in the above?<br>
<br>
Why do I have to add an \n to the print line?<br>
<br>
$ p6 &#39;my @x =3D &lt;&lt;aaa\n bbb\n ccc\n&gt;&gt;; for @x {&quot;$_\n&q=
uot;.print};&#39;<br>
aaa<br>
bbb<br>
ccc<br>
<br>
Oh I see, because they are not actually in the cell:<br>
<br>
$ p6 &#39;my @x =3D &lt;&lt;aaa\n bbb\n ccc\n&gt;&gt;; dd @x&#39;<br>
Array @x =3D [&quot;aaa&quot;, &quot;bbb&quot;, &quot;ccc&quot;]<br>
</blockquote></div><br clear=3D"all"><br>-- <br><div dir=3D"ltr" class=3D"g=
mail_signature">Fernando Santagata</div>

--000000000000882a9c05b4209bfc--
0
nando
11/15/2020 7:56:53 AM
On 2020-11-14 23:56, Fernando Santagata wrote:
> Oh, now I see: you were asking that question in another thread.

I was asking why the \n came out literal in another thread.
It did not help I made a syntax boobo.


I never got the other ways of writing out the array to
work either.
0
perl6
11/15/2020 7:59:47 AM
Reply: