First part of my previous email on this thread! Re-read this bit > First, you were looking at the docs for Path's "lines", but you are > using a string "lines" and those docs say > > multi method lines(Str:D: $limit, :$chomp = True) > multi method lines(Str:D: :$chomp = True) > > Files get "nl-in" due to the special case of text files having various > line endings to tweak. > > Strings already have "split" and "comb" for all the flexibility one may > need there, and what you're playing with is more naturally > dd $_ for $x.split("\t"); # "a","b", ... removes \t > dd $_ for $x.split(/<?after \t>/); "a\t","b\t", ....
and restating the above: "string".lines is different a different method from "File.txt".IO.lines, which is also different from "File.txt".IO.open.lines . Yes that's confusing but there are reasons for all that which make sense when one thinks about them a while. So if you want to split a string, use split! If you want to experiment a text file's line endings with "lines", do that experiment with a file! Which was the 2nd part of my previous email > dd $_ for 'line0-10.txt'.IO.lines(:nl-in["i","\n"], :!chomp)[0..3]; > "Li" > "ne 0\n" > "Li" > "ne 1\n" -y On Mon, Aug 31, 2020 at 5:12 PM ToddAndMargo via perl6-users < perl6-us...@perl.org> wrote: > On 2020-08-30 08:42, yary wrote: > > You were close! > > > > First, you were looking at the docs for Path's "lines", but you are > > using a string "lines" and those docs say > > > > multi method lines(Str:D: $limit, :$chomp = True) > > multi method lines(Str:D: :$chomp = True) > > > > Files get "nl-in" due to the special case of text files having various > > line endings to tweak. > > > > Strings already have "split" and "comb" for all the flexibility one may > > need there, and what you're playing with is more naturally > > dd $_ for $x.split("\t"); # "a","b", ... removes \t > > dd $_ for $x.split(/<?after \t>/); "a\t","b\t", .... > > > > Now back to the Path lines, which DOES let you specify line endings > > > > methodlines(IO::Path:D::$chomp=True, :$enc='utf8', :$nl-in= ["\x0A", > > "\r\n"], |c-->Seq:D) > > > > $ cat line0-10.txt > > Line 0 > > Line 1 > > Line 2 > > ... > > > > let's pretend that the letter "i" is a line ending. > > named arguments can be conveniently written colon pairs :-) > > > > my $nl-in="i"; dd $_ for 'line0-10.txt'.IO.lines(:$nl-in)[0..3]; > > "L" > > "ne 0\nL" > > "ne 1\nL" > > "ne 2\nL" > > > > How about splitting on either "i" or "\n", and not chomping > > > > my $nl-in=("i","\n"); dd $_ for 'line0-10.txt'.IO.lines(:$nl-in, > > :!chomp)[0..3]; > > "Li" > > "ne 0\n" > > "Li" > > "ne 1\n" > > > > To put in line endings without having a variable of the same name as the > > naed arg, use the full form of the colon pair > > dd $_ for 'line0-10.txt'.IO.lines(:nl-in["i","\n"], :!chomp)[0..3]; > > "Li" > > "ne 0\n" > > "Li" > > "ne 1\n" > > > > > > > > -y > > Hi Yary, > > Now what am I doing wrong? > > p6 'my $x="a\tb\tc\td\t"; dd $x; for $x.lines(:nl-in["\t", 0x09], > :!chomp) {dd $_};' > > Str $x = "a\tb\tc\td\t" > > "a\tb\tc\td\t" > > :'( > -T >