On Sat, Aug 29, 2020 at 10:15 PM ToddAndMargo via perl6-users
<perl6-us...@perl.org> 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

Many thanks,
-T

On 2020-08-29 23:10, William Michels via perl6-users wrote:
Maybe this is what you want?

~$ raku -e 'say "test_lines.txt".IO.lines;'
(Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 Line 9 Line 10 Line 11)

~$ raku -e 'say "test_lines.txt".IO.lines.join("\n");'
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Line 11

~$ raku -e 'say "test_lines.txt".IO.lines[3,2].join("\n");'
Line 4
Line 3

~$ raku -e 'say "test_lines.txt".IO.lines[ (3,2).map(*-1) ].join("\n");'
Line 3
Line 2

By default, Raku strips new lines off when reading data in, and adds
newlines back on when outputting with "say" or "put" (but not
"print"). Using "WHAT" you can get an idea how Raku behaves
differently depending on whether you call "IO.lines" or "for lines()"
on an input file:

~$ raku -e '"test_lines.txt".IO.lines.WHAT.say;'
(Seq)

~$ raku -e '"test_lines.txt".IO.lines[3,2].WHAT.say;'
(List)

~$ cat "test_lines.txt" | raku -e 'for lines()[3,2] {.WHAT.say};'
(Str)
(Str)


HTH, Bill.


Hi Bill,

Actually what I am after is to learn

https://docs.raku.org/type/IO::Path#method_lines
method lines(IO::Path:D: :$chomp = True, :$enc = 'utf8', :$nl-in = ["\x0A", "\r\n"], |c --> Seq:D)

Specifically :$chomp = True, :$enc = 'utf8', :$nl-in, and |c.

But, I do love the examples you sent!

Thank you,
-T



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

Reply via email to