Re: spurt and array question

2020-11-15 Thread ToddAndMargo via perl6-users

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.


Re: spurt and array question

2020-11-14 Thread Fernando Santagata
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 <>
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-us...@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 = <>; 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 = <>; for @x {"$_\n".print};'
> aaa
> bbb
> ccc
>
> Oh I see, because they are not actually in the cell:
>
> $ p6 'my @x = <>; dd @x'
> Array @x = ["aaa", "bbb", "ccc"]
>


-- 
Fernando Santagata


Re: spurt and array question

2020-11-14 Thread ToddAndMargo via perl6-users

On 2020-11-14 13:39, Fernando Santagata wrote:

What do you mean by putting the \n in the variable?


$ p6 'my @x = <>; 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 = <>; for @x {"$_\n".print};'
aaa
bbb
ccc

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

$ p6 'my @x = <>; dd @x'
Array @x = ["aaa", "bbb", "ccc"]


Re: spurt and array question

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

> > 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";
>
> 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? [¹]

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

or this?

my @a = ;
'test'.IO.spurt(@a »~» "\n");


[¹] Mind that the array is first converted into a string and its elements
are joined together with an interleaving space

-- 
Fernando Santagata


Re: spurt and array question

2020-11-14 Thread ToddAndMargo via perl6-users
On Sat, Nov 14, 2020 at 1:07 PM ToddAndMargo via perl6-users 
mailto:perl6-us...@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.

 > 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.




On 2020-11-14 11:51, Brad Gilbert wrote:

Actually no I'm not “forgetting that spurt comes with an `:append` option”.
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;
     }



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

Wonderful examples.  Thank you!


Re: spurt and array question

2020-11-14 Thread ToddAndMargo via perl6-users

On 2020-11-14 11:22, Fernando Santagata wrote:
On Sat, Nov 14, 2020 at 8:07 PM ToddAndMargo via perl6-users 
mailto:perl6-us...@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


That is the way around the issue.

But my question is why can I not put the \n in the variable?


Re: spurt and array question

2020-11-14 Thread ToddAndMargo via perl6-users

On 2020-11-14 03:15, Tom Browder wrote:
On Sat, Nov 14, 2020 at 01:59 ToddAndMargo via perl6-users 
mailto:perl6-us...@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




   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 ); }


Re: spurt and array question

2020-11-14 Thread Brad Gilbert
Actually no I'm not “forgetting that spurt comes with an `:append` option”.
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-us...@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.
>
> > 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.
>


Re: spurt and array question

2020-11-14 Thread ToddAndMargo via perl6-users

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
~~


Re: spurt and array question

2020-11-14 Thread Fernando Santagata
On Sat, Nov 14, 2020 at 8:07 PM ToddAndMargo via perl6-users <
perl6-us...@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


Re: spurt and array question

2020-11-14 Thread ToddAndMargo via perl6-users

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.


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.


Re: spurt and array question

2020-11-14 Thread Brad Gilbert
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-us...@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
> ~~
>


Re: spurt and array question

2020-11-14 Thread Gianni Ceccarelli
On 2020-11-13 ToddAndMargo via perl6-users  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 - 
GPG public key fingerprint = A071 E618 DD2C 5901 9574
 6FE2 40EA 9883 7519 3F88
key id = 0x75193F88


Re: spurt and array question

2020-11-14 Thread Tom Browder
On Sat, Nov 14, 2020 at 01:59 ToddAndMargo via perl6-users <
perl6-us...@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


spurt and array question

2020-11-13 Thread ToddAndMargo via perl6-users

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
~~