Re: eof ?

2018-10-09 Thread ToddAndMargo via perl6-users


On 10/9/18 5:42 AM, Fernando Santagata wrote:

The answer Laurent Roseenfeld gave you works for read and readchars as well.
Save the following lines in a file and run it (try and change .read into
.readchars too); it will output a series of 10-byte long Buf[uint8]s,
until it reaches the end of file.

#!/usr/bin/env perl6
given $*PROGRAM-NAME.IO.open {
while my $bytes = .read: 10 {
  $bytes.say;
}
}

On Tue, Oct 9, 2018 at 10:17 AM ToddAndMargo via perl6-users
mailto:perl6-users@perl.org>> wrote:

 On 10/9/18 1:02 AM, ToddAndMargo via perl6-users wrote:
  > Hi All,
  >
  > When reading a text file
  > https://docs.perl6.org/routine/lines
  > seems pretty straight forward.
  >
  > Question:  How do I tell when I when I have
  > reached the EOF (End Of File)?
  >
  > Many thanks,
  > -T

 Please expand the question to include `read` and `readchars`.



--
Fernando Santagata


Hi Frenando,

Thank you for the help!

I am not getting anywhere with `.lines`.  Read the whole thing in the
first line.

$ p6 'my $fh=open "/home/linuxutil/WhoIsMySub.pl6", :r;  while my $f =
$fh.lines { say "$f\n"}; $fh.close;'

#!/usr/bin/env perl6  sub f() { put &?ROUTINE.gist; };  sub abc () {
say "This subroutine's ID is ", f; print "\n";  &?ROUTINE.gist
~~ m/' '(.*?)' '\(/;  my $SubName = $0; say "This subroutine is
called $SubName"; }  abc;

-T



On 10/9/18 7:03 AM, Brad Gilbert wrote:

That isn't the syntax for a loop local variable in Perl 6.

You are trying to use the Perl 5 syntax, which is not going to work in Perl 6

This is the Perl 5 code you are trying to write

 while( my $f = readline $fh ){ say "$f\n"}

Which actually would turn into the following by Perl 5 compiler

 while( defined( my $f = readline $fh ) ){ say "$f\n"}

If you want something that works the same in Perl 6:

 while $fh.get -> $f { say $f }

---

The reason you use `while` in Perl 5 is to prevent `for` from reading
the entire file before looping.

 # Perl 5
 # reads in entire file before doing any work on it
 for my $f (readline $fh){ say $f }
 # (the reason is that readline is in list context)

This is not a problem in Perl 6

 # Perl 6
 # loops over a Seq which only reads enough data to get the next line
 for $fh.lines -> $f { say $f }
On Tue, Oct 9, 2018 at 7:49 AM ToddAndMargo via perl6-users
 wrote:


Hi Brad,

That explains it.  Thank you!

-T


Re: eof ?

2018-10-09 Thread ToddAndMargo via perl6-users
Le mar. 9 oct. 2018 à 14:49, ToddAndMargo via perl6-users 
mailto:perl6-users@perl.org>> a écrit :


On 10/9/18 5:42 AM, Fernando Santagata wrote:
 > The answer Laurent Roseenfeld gave you works for read and
readchars as well.
 > Save the following lines in a file and run it (try and change
.read into
 > .readchars too); it will output a series of 10-byte long
Buf[uint8]s,
 > until it reaches the end of file.
 >
 > #!/usr/bin/env perl6
 > given $*PROGRAM-NAME.IO.open {
 >while my $bytes = .read: 10 {
 >  $bytes.say;
 >}
 > }
 >
 > On Tue, Oct 9, 2018 at 10:17 AM ToddAndMargo via perl6-users
 > mailto:perl6-users@perl.org>
>> wrote:
 >
 > On 10/9/18 1:02 AM, ToddAndMargo via perl6-users wrote:
 >  > Hi All,
 >  >
 >  > When reading a text file
 >  > https://docs.perl6.org/routine/lines
 >  > seems pretty straight forward.
 >  >
 >  > Question:  How do I tell when I when I have
 >  > reached the EOF (End Of File)?
 >  >
 >  > Many thanks,
 >  > -T
 >
 > Please expand the question to include `read` and `readchars`.
 >
 >
 >
 > --
 > Fernando Santagata

Hi Frenando,

Thank you for the help!

I am not getting anywhere with `.lines`.  Read the whole thing in the
first line.

$ p6 'my $fh=open "/home/linuxutil/WhoIsMySub.pl6", :r;  while my $f =
$fh.lines { say "$f\n"}; $fh.close;'

#!/usr/bin/env perl6  sub f() { put &?ROUTINE.gist; };  sub abc () {
say "This subroutine's ID is ", f; print "\n";  &?ROUTINE.gist
~~ m/' '(.*?)' '\(/;  my $SubName = $0; say "This subroutine is
called $SubName"; }  abc;

-T



On 10/9/18 6:26 AM, Laurent Rosenfeld via perl6-users wrote:

This:
my $f = $fh.lines;
will slurp all the lines into $f (but you can still access the 
individual items with something like $f[4]).


So you don't want to use this in a while loop, since everything will be 
consumed during the first loop iteration. Either use a for loop to 
process the lines one by one (as shown in my previous answer), and the 
for loop will stop once you've read the whole file, and this is probably 
the way to go if your file is large:

for "test.txt".IO.lines -> $c { say $c }
or possibly
.say for "text.txt".IO.lines;
or you can dump all the lines into an array (and later process the array):
my @f = "test.txt".IO.lines;


My misunderstanding of .lines.  Thank you!


Re: eof ?

2018-10-09 Thread ToddAndMargo via perl6-users

On 10/9/18 6:22 AM, Curt Tilmes wrote:


On Tue, Oct 9, 2018 at 8:49 AM ToddAndMargo via perl6-users 
mailto:perl6-users@perl.org>> wrote:



I am not getting anywhere with `.lines`.  Read the whole thing in the
first line.

$ p6 'my $fh=open "/home/linuxutil/WhoIsMySub.pl6", :r;  while my $f =
$fh.lines { say "$f\n"}; $fh.close;'


.lines() creates Seq (https://docs.perl6.org/type/Seq) that is a lazy 
list of all the lines in the file.


Your while loop calls it once, which makes that Seq, Inside your first 
loop, which prints it, the Stringify iterates the Seq, printing all the 
lines.
The second call to .lines() returns nothing because you're now at eof, 
no more lines.


Note the Seq that .lines returns itself is lazy.  It doesn't 
(necessarily) have all the lines, just the capability to get them.

As you iterate the Seq, it pulls in all the lines.

Curt



Hi Curt,

I was thinking that .lines read them one at a time.

Thank you!

-T
--
~~
Computers are like air conditioners.
They malfunction when you open windows
~~


Re: eof ?

2018-10-09 Thread Brad Gilbert
That isn't the syntax for a loop local variable in Perl 6.

You are trying to use the Perl 5 syntax, which is not going to work in Perl 6

This is the Perl 5 code you are trying to write

while( my $f = readline $fh ){ say "$f\n"}

Which actually would turn into the following by Perl 5 compiler

while( defined( my $f = readline $fh ) ){ say "$f\n"}

If you want something that works the same in Perl 6:

while $fh.get -> $f { say $f }

---

The reason you use `while` in Perl 5 is to prevent `for` from reading
the entire file before looping.

# Perl 5
# reads in entire file before doing any work on it
for my $f (readline $fh){ say $f }
# (the reason is that readline is in list context)

This is not a problem in Perl 6

# Perl 6
# loops over a Seq which only reads enough data to get the next line
for $fh.lines -> $f { say $f }
On Tue, Oct 9, 2018 at 7:49 AM ToddAndMargo via perl6-users
 wrote:
>
> On 10/9/18 5:42 AM, Fernando Santagata wrote:
> > The answer Laurent Roseenfeld gave you works for read and readchars as well.
> > Save the following lines in a file and run it (try and change .read into
> > .readchars too); it will output a series of 10-byte long Buf[uint8]s,
> > until it reaches the end of file.
> >
> > #!/usr/bin/env perl6
> > given $*PROGRAM-NAME.IO.open {
> >while my $bytes = .read: 10 {
> >  $bytes.say;
> >}
> > }
> >
> > On Tue, Oct 9, 2018 at 10:17 AM ToddAndMargo via perl6-users
> > mailto:perl6-users@perl.org>> wrote:
> >
> > On 10/9/18 1:02 AM, ToddAndMargo via perl6-users wrote:
> >  > Hi All,
> >  >
> >  > When reading a text file
> >  > https://docs.perl6.org/routine/lines
> >  > seems pretty straight forward.
> >  >
> >  > Question:  How do I tell when I when I have
> >  > reached the EOF (End Of File)?
> >  >
> >  > Many thanks,
> >  > -T
> >
> > Please expand the question to include `read` and `readchars`.
> >
> >
> >
> > --
> > Fernando Santagata
>
> Hi Frenando,
>
> Thank you for the help!
>
> I am not getting anywhere with `.lines`.  Read the whole thing in the
> first line.
>
> $ p6 'my $fh=open "/home/linuxutil/WhoIsMySub.pl6", :r;  while my $f =
> $fh.lines { say "$f\n"}; $fh.close;'
>
> #!/usr/bin/env perl6  sub f() { put &?ROUTINE.gist; };  sub abc () {
> say "This subroutine's ID is ", f; print "\n";  &?ROUTINE.gist
> ~~ m/' '(.*?)' '\(/;  my $SubName = $0; say "This subroutine is
> called $SubName"; }  abc;
>
> -T


Re: eof ?

2018-10-09 Thread Brad Gilbert
On Tue, Oct 9, 2018 at 8:31 AM Curt Tilmes  wrote:
>
> On Tue, Oct 9, 2018 at 9:27 AM Laurent Rosenfeld via perl6-users 
>  wrote:
>>
>> This:
>> my $f = $fh.lines;
>> will slurp all the lines into $f (but you can still access the individual 
>> items with something like $f[4]).
>
>
> Is that true?  I supposed that it would hold the Seq as a scalar 
> (un-iterated) Seq.

No it isn't true.
It stores the Seq in $f.

The following doesn't print anything.

my $f = 'example.txt'.IO.lines.map(*.say); Nil

(Note that I added `Nil` so that if someone tried this in the REPL, it
doesn't try to `say` what's in `$f`.)

What it does do is make the Seq marked as an item, so this only runs
one loop, and doesn't run the `.map` code.

for $f { Nil }

But both of these will:

for @$f { Nil }
for $f<> { Nil }


> I know that my @f = $fh.lines will slurp it all, but I thought you could 
> avoid that by assigning it to a scalar.
>
> Curt
>


Re: eof ?

2018-10-09 Thread Laurent Rosenfeld via perl6-users
Yes, you're right, it is a Seq. I was trying to be pedagogical, but
probably wasn't very accurate. It is a Seq, and the "slurping" will be lazy.



Le mar. 9 oct. 2018 à 15:30, Curt Tilmes  a écrit :

> On Tue, Oct 9, 2018 at 9:27 AM Laurent Rosenfeld via perl6-users <
> perl6-users@perl.org> wrote:
>
>> This:
>> my $f = $fh.lines;
>> will slurp all the lines into $f (but you can still access the individual
>> items with something like $f[4]).
>>
>
> Is that true?  I supposed that it would hold the Seq as a scalar
> (un-iterated) Seq.
>
> I know that my @f = $fh.lines will slurp it all, but I thought you could
> avoid that by assigning it to a scalar.
>
> Curt
>
>


Re: eof ?

2018-10-09 Thread Curt Tilmes
On Tue, Oct 9, 2018 at 9:27 AM Laurent Rosenfeld via perl6-users <
perl6-users@perl.org> wrote:

> This:
> my $f = $fh.lines;
> will slurp all the lines into $f (but you can still access the individual
> items with something like $f[4]).
>

Is that true?  I supposed that it would hold the Seq as a scalar
(un-iterated) Seq.

I know that my @f = $fh.lines will slurp it all, but I thought you could
avoid that by assigning it to a scalar.

Curt


Re: eof ?

2018-10-09 Thread Laurent Rosenfeld via perl6-users
This:
my $f = $fh.lines;
will slurp all the lines into $f (but you can still access the individual
items with something like $f[4]).

So you don't want to use this in a while loop, since everything will be
consumed during the first loop iteration. Either use a for loop to process
the lines one by one (as shown in my previous answer), and the for loop
will stop once you've read the whole file, and this is probably the way to
go if your file is large:
for "test.txt".IO.lines -> $c { say $c }
or possibly
.say for "text.txt".IO.lines;
or you can dump all the lines into an array (and later process the array):
my @f =  "test.txt".IO.lines;


Le mar. 9 oct. 2018 à 14:49, ToddAndMargo via perl6-users <
perl6-users@perl.org> a écrit :

> On 10/9/18 5:42 AM, Fernando Santagata wrote:
> > The answer Laurent Roseenfeld gave you works for read and readchars as
> well.
> > Save the following lines in a file and run it (try and change .read into
> > .readchars too); it will output a series of 10-byte long Buf[uint8]s,
> > until it reaches the end of file.
> >
> > #!/usr/bin/env perl6
> > given $*PROGRAM-NAME.IO.open {
> >while my $bytes = .read: 10 {
> >  $bytes.say;
> >}
> > }
> >
> > On Tue, Oct 9, 2018 at 10:17 AM ToddAndMargo via perl6-users
> > mailto:perl6-users@perl.org>> wrote:
> >
> > On 10/9/18 1:02 AM, ToddAndMargo via perl6-users wrote:
> >  > Hi All,
> >  >
> >  > When reading a text file
> >  > https://docs.perl6.org/routine/lines
> >  > seems pretty straight forward.
> >  >
> >  > Question:  How do I tell when I when I have
> >  > reached the EOF (End Of File)?
> >  >
> >  > Many thanks,
> >  > -T
> >
> > Please expand the question to include `read` and `readchars`.
> >
> >
> >
> > --
> > Fernando Santagata
>
> Hi Frenando,
>
> Thank you for the help!
>
> I am not getting anywhere with `.lines`.  Read the whole thing in the
> first line.
>
> $ p6 'my $fh=open "/home/linuxutil/WhoIsMySub.pl6", :r;  while my $f =
> $fh.lines { say "$f\n"}; $fh.close;'
>
> #!/usr/bin/env perl6  sub f() { put &?ROUTINE.gist; };  sub abc () {
> say "This subroutine's ID is ", f; print "\n";  &?ROUTINE.gist
> ~~ m/' '(.*?)' '\(/;  my $SubName = $0; say "This subroutine is
> called $SubName"; }  abc;
>
> -T
>


Re: eof ?

2018-10-09 Thread Curt Tilmes
On Tue, Oct 9, 2018 at 8:49 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

>
> I am not getting anywhere with `.lines`.  Read the whole thing in the
> first line.
>
> $ p6 'my $fh=open "/home/linuxutil/WhoIsMySub.pl6", :r;  while my $f =
> $fh.lines { say "$f\n"}; $fh.close;'
>

.lines() creates Seq (https://docs.perl6.org/type/Seq) that is a lazy list
of all the lines in the file.

Your while loop calls it once, which makes that Seq, Inside your first
loop, which prints it, the Stringify iterates the Seq, printing all the
lines.
The second call to .lines() returns nothing because you're now at eof, no
more lines.

Note the Seq that .lines returns itself is lazy.  It doesn't (necessarily)
have all the lines, just the capability to get them.
As you iterate the Seq, it pulls in all the lines.

Curt


Re: eof ?

2018-10-09 Thread ToddAndMargo via perl6-users

On 10/9/18 5:42 AM, Fernando Santagata wrote:

The answer Laurent Roseenfeld gave you works for read and readchars as well.
Save the following lines in a file and run it (try and change .read into 
.readchars too); it will output a series of 10-byte long Buf[uint8]s, 
until it reaches the end of file.


#!/usr/bin/env perl6
given $*PROGRAM-NAME.IO.open {
   while my $bytes = .read: 10 {
     $bytes.say;
   }
}

On Tue, Oct 9, 2018 at 10:17 AM ToddAndMargo via perl6-users 
mailto:perl6-users@perl.org>> wrote:


On 10/9/18 1:02 AM, ToddAndMargo via perl6-users wrote:
 > Hi All,
 >
 > When reading a text file
 > https://docs.perl6.org/routine/lines
 > seems pretty straight forward.
 >
 > Question:  How do I tell when I when I have
 > reached the EOF (End Of File)?
 >
 > Many thanks,
 > -T

Please expand the question to include `read` and `readchars`.



--
Fernando Santagata


Hi Frenando,

Thank you for the help!

I am not getting anywhere with `.lines`.  Read the whole thing in the 
first line.


$ p6 'my $fh=open "/home/linuxutil/WhoIsMySub.pl6", :r;  while my $f = 
$fh.lines { say "$f\n"}; $fh.close;'


#!/usr/bin/env perl6  sub f() { put &?ROUTINE.gist; };  sub abc () { 
say "This subroutine's ID is ", f; print "\n";  &?ROUTINE.gist 
~~ m/' '(.*?)' '\(/;  my $SubName = $0; say "This subroutine is 
called $SubName"; }  abc;


-T


Re: eof ?

2018-10-09 Thread ToddAndMargo via perl6-users

On 10/9/18 5:03 AM, ToddAndMargo via perl6-users wrote:
Le mar. 9 oct. 2018 à 10:03, ToddAndMargo via perl6-users 
mailto:perl6-users@perl.org>> a écrit :


    Hi All,

    When reading a text file
    https://docs.perl6.org/routine/lines
    seems pretty straight forward.

    Question:  How do I tell when I when I have
    reached the EOF (End Of File)?

    Many thanks,
    -T



On 10/9/18 4:38 AM, Laurent Rosenfeld via perl6-users wrote:
The eof method of the IO::Handle class returns True if you exhausted 
the contents of the handle, but you generally don't need to use that, 
since something like:


for'input.txt'.IO.lines->$line{
# Do something with $line
}

will gracefully handle ends of files for you without you having to do 
anything special.


Hi Laurent,

Thank you!

So loop until lines return false?

Is it just me, or is this not documented on

  https://docs.perl6.org/routine/lines
?

-T



Where in the following would I put a echo of the True / False
return from `lines`?

$ p6 'my $fh=open "/home/linuxutil/WhoIsMySub.pl6", :r;  for $fh.lines { 
say $_}; $fh.close;'


Re: eof ?

2018-10-09 Thread Fernando Santagata
The answer Laurent Roseenfeld gave you works for read and readchars as well.
Save the following lines in a file and run it (try and change .read into
.readchars too); it will output a series of 10-byte long Buf[uint8]s, until
it reaches the end of file.

#!/usr/bin/env perl6
given $*PROGRAM-NAME.IO.open {
  while my $bytes = .read: 10 {
$bytes.say;
  }
}

On Tue, Oct 9, 2018 at 10:17 AM ToddAndMargo via perl6-users <
perl6-users@perl.org> wrote:

> On 10/9/18 1:02 AM, ToddAndMargo via perl6-users wrote:
> > Hi All,
> >
> > When reading a text file
> >  https://docs.perl6.org/routine/lines
> > seems pretty straight forward.
> >
> > Question:  How do I tell when I when I have
> > reached the EOF (End Of File)?
> >
> > Many thanks,
> > -T
>
> Please expand the question to include `read` and `readchars`.
>


-- 
Fernando Santagata


Re: eof ?

2018-10-09 Thread ToddAndMargo via perl6-users
Le mar. 9 oct. 2018 à 10:03, ToddAndMargo via perl6-users 
mailto:perl6-users@perl.org>> a écrit :


Hi All,

When reading a text file
https://docs.perl6.org/routine/lines
seems pretty straight forward.

Question:  How do I tell when I when I have
reached the EOF (End Of File)?

Many thanks,
-T



On 10/9/18 4:38 AM, Laurent Rosenfeld via perl6-users wrote:
The eof method of the IO::Handle class returns True if you exhausted the 
contents of the handle, but you generally don't need to use that, since 
something like:


for'input.txt'.IO.lines->$line{
# Do something with $line
}

will gracefully handle ends of files for you without you having to do 
anything special.


Hi Laurent,

Thank you!

So loop until lines return false?

Is it just me, or is this not documented on

 https://docs.perl6.org/routine/lines
?

-T


Re: eof ?

2018-10-09 Thread Laurent Rosenfeld via perl6-users
The eof method of the IO::Handle class returns True if you exhausted the
contents of the handle, but you generally don't need to use that, since
something like:

for 'input.txt'.IO.lines -> $line {
# Do something with $line
}

will gracefully handle ends of files for you without you having to do
anything special.



Le mar. 9 oct. 2018 à 10:03, ToddAndMargo via perl6-users <
perl6-users@perl.org> a écrit :

> Hi All,
>
> When reading a text file
>  https://docs.perl6.org/routine/lines
> seems pretty straight forward.
>
> Question:  How do I tell when I when I have
> reached the EOF (End Of File)?
>
> Many thanks,
> -T
>


Re: eof ?

2018-10-09 Thread ToddAndMargo via perl6-users

On 10/9/18 1:02 AM, ToddAndMargo via perl6-users wrote:

Hi All,

When reading a text file
     https://docs.perl6.org/routine/lines
seems pretty straight forward.

Question:  How do I tell when I when I have
reached the EOF (End Of File)?

Many thanks,
-T


Please expand the question to include `read` and `readchars`.