.say for lines

On Mon, Jan 20, 2020 at 1:59 AM William Michels via perl6-users <
perl6-us...@perl.org> wrote:

> Hi Yary (and Todd),
>
> Thank you both for your responses. Yary, the problem seems to be with
> "get". I can change 'while' to 'for' below, but using 'get' raku/perl6
> actually returns fewer lines with "for" than it did with "while":
>
> [1]mydir$ cat testthis_abc_def.txt
> a
> b
> c
>
> d
> e
> f
> [2]mydir$ perl6 -e '.say while $_ = $*IN.get;' < testthis_abc_def.txt
> a
> b
> c
> [3]mydir$ perl6 -e '.say for $_ = $*IN.get;' < testthis_abc_def.txt
> a
>
> I assumed 'get' was looking at EOF but that's incorrect--apparently
> 'get' acts more like a glorified call to lines[0], reading one line of
> an input file and then stopping. I can read my whole test file by
> explicitly testing for EOF (note the redeclaration of $*IN below).
> Otherwise, my best course of action is replacing 'get' with 'lines',
> and using either 'while' or 'for' ('.say for $*IN.lines;' replicates
> 'cat' behavior the best):
>
> [4]mydir$ perl6 -e 'while not($*IN.eof) -> {$*IN.get.say };' <
> testthis_abc_def.txt
> a
> b
> c
>
> d
> e
> f
> [5]mydir$ perl6 -e '.say while $_ =$*IN.lines;' < testthis_abc_def.txt
> (a b c  d e f)
> [6]mydir$ perl6 -e '.say for $_ = $*IN.lines;' < testthis_abc_def.txt
> (a b c  d e f)
> [7]mydir$ perl6 -e '.say for $*IN.lines;' < testthis_abc_def.txt
> a
> b
> c
>
> d
> e
> f
>
> HTH, Bill.
>
>
>
>
>
> On Sat, Jan 18, 2020 at 5:55 PM yary <not....@gmail.com> wrote:
> >
> > "while" is the wrong looping construct for going over file lines, and
> that's across a great many computer languages. It will stop when it
> encounters a false line- typically an empty line or '0'
> >
> > Try "for"
> >
> > -y
> >
> >
> > On Sat, Jan 18, 2020 at 4:45 PM William Michels <w...@caa.columbia.edu>
> wrote:
> >>
> >> Hello All,
> >>
> >> I've been reviewing literature that discusses using raku/perl6 as a
> >> replacement for common unix utilities. One important unix utility is
> >> "cat". I looked at docs/blogs and found a recommendation to use "$*IN"
> >> along with "slurp" (references at bottom). Using a seven-line test
> >> file "testthis_abc_def.txt" below (1), the recommended "slurp" code
> >> works as expected (2).
> >>
> >> However, another recommendation to use "$*IN" along with the "get"
> >> method fails when a blank line is encountered, only returning
> >> truncated output (3). I tried correcting truncated output seen with
> >> "get" by playing with the command-line arguments "-ne" (4) and "-pe"
> >> (5), but only ended up mangling output even further.
> >>
> >> Can "get" be used in when writing raku/perl6 replacement code for "cat"?
> >>
> >> Any advice appreciated,
> >>
> >> Bill.
> >>
> >>
> >> [1]mydir$ cat testthis_abc_def.txt
> >> a
> >> b
> >> c
> >>
> >> d
> >> e
> >> f
> >> [2]mydir$ perl6 -e 'say $*IN.slurp;' < testthis_abc_def.txt
> >> a
> >> b
> >> c
> >>
> >> d
> >> e
> >> f
> >>
> >> [3]mydir$ perl6 -e '.say while $_ = $*IN.get;' < testthis_abc_def.txt
> >> a
> >> b
> >> c
> >> [4]mydir$ perl6 -ne '.say while $_ = $*IN.get;' < testthis_abc_def.txt
> >> b
> >> c
> >> e
> >> f
> >> [5]mydir$ perl6 -pe '.say while $_ = $*IN.get;' < testthis_abc_def.txt
> >> b
> >> c
> >>
> >> e
> >> f
> >> (Mu)
> >> [6]mydir$
> >>
> >>
> >> REFERENCES:
> >> 1. https://docs.raku.org/routine/slurp
> >> 2. https://docs.raku.org/routine/get
> >> 3.
> https://andrewshitov.com/2019/09/09/the-cat-utility-written-in-perl-6/
> >> 4.
> https://stackoverflow.com/questions/52597984/catching-exception-of-a-shell-command-in-perl-6
>

Reply via email to