On 2020-08-29 23:09, Tobias Boege wrote:
On Sat, 29 Aug 2020, ToddAndMargo via perl6-users wrote:
Hi All,
I am trying to figure out how to use line with :$chomp.
Now what am I doing wrong?
$ alias p6
alias p6='perl6 -e'
$ p6 'say "Lines.txt".IO.open.lines(:chomp)[3,2];'
(Line 3 Line 2)
$ p6 'say "Lines.txt".IO.open.lines(:!chomp)[3,2];'
(Line 3 Line 2)
I am looking for
Line 3
Line 2
Then I would suggest
say $_ for "Lines.txt".IO.lines[3,2]
Now what am I doing wrong?
- You are calling .lines on the value of .IO.open which is an
IO::Handle. IO::Handle.lines does not take a named argument
:chomp, so passing one is useless.
That explains it.
Butttttttt:
https://docs.raku.org/type/IO::Path#method_lines
(IO::Path) method lines
Defined as:
method lines(IO::Path:D: :$chomp = True, :$enc = 'utf8', :$nl-in =
["\x0A", "\r\n"], |c --> Seq:D)
Opens the invocant and returns its lines.
The behavior is equivalent to opening the file specified
by the invocant, forwarding the :$chomp, :$enc, and
:$nl-in arguments to IO::Handle.open, then calling
IO::Handle.lines on that handle, forwarding any of
the remaining arguments to that method, and returning
the resultant Seq.
The "signature" line (cryptogram) clearly stated that
":$chomp" is a parameter of the method.
Now you are obviously correct that :chomp is ignored.
If I am not mistaken, I have just tripped over another
error in the documentation. Your take?
Now to beat a dead horse some more, who in the right
mind thinks this example belongs as an example of
"(IO::Path) method lines"? It does not show how to
use any of the options and is a far better example
of how to use "grep" and "elems" than "(IO::Path)
method lines". Me thinks someone was showing off.
say "The file contains ",
'50GB-file'.IO.lines.grep(*.contains: 'Perl').elems,
" lines that mention Perl";
# OUTPUT: «The file contains 72 lines that mention Perl»
The .lines method on an
IO::Path, however, which I call in my snippet, supports
such an argument. You could call .IO.lines(:chomp) or
.IO.open(:chomp).lines.
- You should use :chomp and not :!chomp because :chomp removes
the trailing newline from every line read for you. When you
use `say`, a trailing newline is added automatically, so in
order to get only one newline in the output per item, you
have to :chomp. The :chomp argument does not appear in my
snippet because it is the default, but you could have done
say $_ for "Lines.txt".IO.lines(:!chomp)[3,2]
to see the effect of not chomping.
- What you `say` is the return value of indexing the Seq that
is returned by .lines. This is an object of type List and
will always print in the format "(item1 item2 ...)" and not
in the format you desire. In the snippet above I iterate
over that List with `for` and print every item in a new line.
Best,
Tobias
$ p6 'dd "Lines.txt".IO.open(:chomp).lines()[3,2];'
("Line 3", "Line 2")
$ p6 'dd "Lines.txt".IO.open(:!chomp).lines()[3,2];'
("Line 3\n", "Line 2\n")
$ p6 'say "Lines.txt".IO.open(:!chomp).lines()[3,2];'
(Line 3
Line 2 <-- why the space?
)
$ p6 'print "Lines.txt".IO.open(:!chomp).lines()[3,2];'
Line 3
Line 2 <-- why the space?
$ p6 'print "Lines.txt".IO.open(:!chomp).lines()[3,2,5];'
Line 3
Line 2 <-- why the space?
Line 5 <-- why the space?
-T