Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current line number during "raku -n" iteration?

Tags:

raku

In Perl I could:

cat file.txt | perl -nE 'say $.' 

The closest Raku solution i found (without using state counter variable) returns position in bytes:

cat file.txt | raku -ne 'say $*IN.tell' 
like image 874
Pawel Pabian bbkr Avatar asked Mar 12 '20 12:03

Pawel Pabian bbkr


2 Answers

Use a state variable for it and increment that. Most conveniently, there is the anonymous state variable $, meaning one can do:

echo -e "foo\nbar\nbaz" | perl6 -ne 'say $++ ~ ": $_"'

Which gives:

0: foo
1: bar
2: baz

Use ++$ instead for base-1 numbering. There isn't anything special built in to the handle; it falls neatly out of state variables or, for an explicit iteration, .lines.kv, and then it's only paid for by things that want it.

like image 151
Jonathan Worthington Avatar answered Nov 19 '22 15:11

Jonathan Worthington


The LN module is probably what you want to use.

like image 42
Scimon Proctor Avatar answered Nov 19 '22 13:11

Scimon Proctor