Re: liens and :chomp question
On 2020-09-01 11:14, ToddAndMargo via perl6-users wrote: I see Fedora's docs -- which I use a lot -- uses the same idea: https://docs.fedoraproject.org/en-US/fedora/f32/system-administrators-guide/Wayland/ But somehow is a ton more obvious. Reference page: https://docs.fedoraproject.org/en-US/fedora/f32/system-administrators-guide/ Okay, on Fedora's page, there is two panes. Left, with the table of contents, and Right with the body. If you are moused over the left pane, your mouse roller scrolls through the contents. If you are on the Right pane, you scroll through the body. That would be super powerful to do with Raku's docs, as you could rapidly get to each subsection and there would be no "out of sight, out of mind" when you scroll a bit on the body.
Re: liens and :chomp question
On 2020-08-31 21:48, ToddAndMargo wrote: On 2020-08-30 08:16, yary wrote: Looking up https://docs.raku.org/routine/lines shows a Table of Contents with class Cool (Cool) routine lines class Supply (Supply) method lines class Str (Str) routine lines class IO::CatHandle (IO::CatHandle) method lines class IO::Path (IO::Path) method lines class IO::Handle (IO::Handle) routine lines class IO::Socket::INET (IO::Socket::INET) method lines Hi Yary, I am not finding such a critter (table of contents). On mine, you have to scroll though each of them. It would be a wonder addition if the technical writers would stick your list at the top and make them links to each subsection. -T Found it. Andy helped me off line. It is off to the side on https://docs.raku.org/routine/lines I was expecting it to be below the main title. I must have used that page 1,000 times and never realized there was something on the right margin. I was expecting something like that to be under the page's main title. I see Fedora's docs -- which I use a lot -- uses the same idea: https://docs.fedoraproject.org/en-US/fedora/f32/system-administrators-guide/Wayland/ But somehow is a ton more obvious. You learn something every day. :'( -T
Re: liens and :chomp question
On 2020-08-30 08:16, yary wrote: Looking up https://docs.raku.org/routine/lines shows a Table of Contents with class Cool (Cool) routine lines class Supply (Supply) method lines class Str (Str) routine lines class IO::CatHandle (IO::CatHandle) method lines class IO::Path (IO::Path) method lines class IO::Handle (IO::Handle) routine lines class IO::Socket::INET (IO::Socket::INET) method lines Hi Yary, I am not finding such a critter (table of contents). On mine, you have to scroll though each of them. It would be a wonder addition if the technical writers would stick your list at the top and make them links to each subsection. -T
Re: liens and :chomp question
On 2020-08-30 08:16, yary wrote: Expanding on how to read the docs & signature a bit, from Tobias You confuse two methods that have the same name "lines". One of them, which exists in the IO::Path class, has a :chomp argument. The other, on IO::Handle does not. "path".IO.lines() <-- calls lines on an IO::Path (supports :chomp) "path".IO.open.lines() <-- calls lines on an IO::Handle (does not support :chomp) Looking up https://docs.raku.org/routine/lines shows a Table of Contents with class Cool (Cool) routine lines class Supply (Supply) method lines class Str (Str) routine lines class IO::CatHandle (IO::CatHandle) method lines class IO::Path (IO::Path) method lines class IO::Handle (IO::Handle) routine lines class IO::Socket::INET (IO::Socket::INET) method lines Lots of different "lines" methods. If I click on IO::Handle it jumps to sub lines( $what = $*ARGFILES, |c) multi method lines( IO::Handle:D: $limit, :$close ) multi method lines( IO::Handle:D: :$close ) which indeed has no named argument for "chomp" As an aside-Reading between the lines (no pun intended), I deduce that the IO::Handle "lines" method is its own implementation so that it can read "lazily" as needed, and to support the "close" option. And as another aside, it has a "sub" which shows that when "lines" is called with no arguments, it defaults to reading from $*ARGFILES. So, back to the table of contents. How does one know which "lines" routine to look at? Either by thinking & remembering -- "oh 'file'.IO.open returns an IO::Handle that's the one to read" "Huh 'file'.IO returns a path object, and there's /'(IO::Path) method/ /lines'/ listed lets look at that, hmm it has /chomp/" or by experiment! > 'example.txt'.IO.WHAT (Path) > 'example.txt'.IO.open.WHAT (Handle) -y Thank you!
Re: liens and :chomp question
Expanding on how to read the docs & signature a bit, from Tobias > You confuse two methods that have the same name "lines". One of them, > which exists in the IO::Path class, has a :chomp argument. The other, > on IO::Handle does not. > "path".IO.lines() <-- calls lines on an IO::Path (supports :chomp) > "path".IO.open.lines() <-- calls lines on an IO::Handle (does not > support :chomp) Looking up https://docs.raku.org/routine/lines shows a Table of Contents with class Cool (Cool) routine lines class Supply (Supply) method lines class Str (Str) routine lines class IO::CatHandle (IO::CatHandle) method lines class IO::Path (IO::Path) method lines class IO::Handle (IO::Handle) routine lines class IO::Socket::INET (IO::Socket::INET) method lines Lots of different "lines" methods. If I click on IO::Handle it jumps to sub lines( $what = $*ARGFILES, |c) multi method lines( IO::Handle:D: $limit, :$close ) multi method lines( IO::Handle:D: :$close ) which indeed has no named argument for "chomp" As an aside-Reading between the lines (no pun intended), I deduce that the IO::Handle "lines" method is its own implementation so that it can read "lazily" as needed, and to support the "close" option. And as another aside, it has a "sub" which shows that when "lines" is called with no arguments, it defaults to reading from $*ARGFILES. So, back to the table of contents. How does one know which "lines" routine to look at? Either by thinking & remembering -- "oh 'file'.IO.open returns an IO::Handle that's the one to read" "Huh 'file'.IO returns a path object, and there's *'(IO::Path) method* *lines'* listed lets look at that, hmm it has *chomp*" or by experiment! > 'example.txt'.IO.WHAT (Path) > 'example.txt'.IO.open.WHAT (Handle) -y On Sun, Aug 30, 2020 at 12:56 AM ToddAndMargo via perl6-users < perl6-users@perl.org> wrote: > On 2020-08-30 00:35, Tobias Boege wrote: > > On Sun, 30 Aug 2020, ToddAndMargo via perl6-users wrote: > >>> - 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. > >> > >> Bu: > >> 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? > >> > > > > You confuse two methods that have the same name "lines". One of them, > > which exists in the IO::Path class, has a :chomp argument. The other, > > on IO::Handle does not. > > > >"path".IO.lines() <-- calls lines on an IO::Path (supports > :chomp) > >"path".IO.open.lines() <-- calls lines on an IO::Handle (does not > support :chomp) > > > > In the second case, if you want chomping to happen you have to pass > > the :chomp to the *open* call, as you do below: > > Hi Tobias, > > The terrible example they included was: > '50GB-file'.IO.lines.grep(*.contains: 'Perl').elems, >" lines that mention Perl"; > > I left off the "grep" and the "elems". > > "Lines.txt".IO.open.lines > > This was the example they gave for > > method lines(IO::Path:D: :$chomp = True, :$enc = 'utf8', :$nl-in = > ["\x0A", "\r\n"], |c --> Seq:D) > > Exactly what is "IO::Path:D:"? > > I know that :D is declared and the ":" after it > is a delimiter. That leaved "IO::Path": > > https://docs.raku.org/type/IO::Path > > And I can't make heads or tails out of the page. > > My goal is to learn how to use :$chomp = True, > :$enc = 'utf8', :$nl-in, and, |c > > Thank you for the help! > -T >
Re: liens and :chomp question
On 2020-08-30 00:35, Tobias Boege wrote: On Sun, 30 Aug 2020, ToddAndMargo via perl6-users wrote: - 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. Bu: 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? You confuse two methods that have the same name "lines". One of them, which exists in the IO::Path class, has a :chomp argument. The other, on IO::Handle does not. "path".IO.lines() <-- calls lines on an IO::Path (supports :chomp) "path".IO.open.lines() <-- calls lines on an IO::Handle (does not support :chomp) In the second case, if you want chomping to happen you have to pass the :chomp to the *open* call, as you do below: Hi Tobias, The terrible example they included was: '50GB-file'.IO.lines.grep(*.contains: 'Perl').elems, " lines that mention Perl"; I left off the "grep" and the "elems". "Lines.txt".IO.open.lines This was the example they gave for method lines(IO::Path:D: :$chomp = True, :$enc = 'utf8', :$nl-in = ["\x0A", "\r\n"], |c --> Seq:D) Exactly what is "IO::Path:D:"? I know that :D is declared and the ":" after it is a delimiter. That leaved "IO::Path": https://docs.raku.org/type/IO::Path And I can't make heads or tails out of the page. My goal is to learn how to use :$chomp = True, :$enc = 'utf8', :$nl-in, and, |c Thank you for the help! -T
Re: liens and :chomp question
On Sat, Aug 29, 2020 at 10:15 PM 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 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 ~~
Re: liens and :chomp question
On Sun, 30 Aug 2020, ToddAndMargo via perl6-users wrote: > >- 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. > > Bu: > 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? > You confuse two methods that have the same name "lines". One of them, which exists in the IO::Path class, has a :chomp argument. The other, on IO::Handle does not. "path".IO.lines() <-- calls lines on an IO::Path (supports :chomp) "path".IO.open.lines() <-- calls lines on an IO::Handle (does not support :chomp) In the second case, if you want chomping to happen you have to pass the :chomp to the *open* call, as you do below: > $ 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? > ) > The better question is "whose space". You print again the List of lines you obtain by indexing the return value of .lines, so expect output in the form of "(item1 item2 ...)". This is where your space comes from. Lists print that way. They insert spaces between their elements. In this case, you had :chomp disabled, so the "Line 3" item printed "Line 3\n", including the newline that was not chomped, then the space and then the next item, "Line2\n" with its newline, then the closing parenthesis which is also part of printing a List. This serves to better illustrate where one element of the List ends and where the next begins. The space separate them: $ rakudo -e 'say "Lines.txt".IO.open(:!chomp).lines()[3,2].map({ "«$_»" });' («Line 3 » «Line 2 ») Best, Tobias -- "There's an old saying: Don't change anything... ever!" -- Mr. Monk
Re: liens and :chomp question
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. Bu: 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
Re: liens and :chomp question
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. On Sat, Aug 29, 2020 at 10:15 PM 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 > > Many thanks, > -T
Re: liens and :chomp question
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
liens and :chomp question
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