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

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk

Reply via email to