Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I skip the first few lines of a text file using the raku IO.lines.race construct

Tags:

raku

I am trying to read a text file using raku with the IO.lines.race construct. For example

for $file.IO.lines.race
{
  #do something, such as
  my ($a,$b)=.split(" ");
}

How can I skip the, say, first three lines of the text file?

Thanks!

Tao

like image 956
Tao Wang Avatar asked Mar 04 '20 03:03

Tao Wang


2 Answers

The answer above by Lukas Valle is perfectly fine; you can use skip to skip the lines you don't need. However, I can't help but indicate that case goes much better with other functional constructs such as map:

$file.IO.lines.skip(3).race.map( .split(" ") );

That way, you can chain several operations together without creating different loops. Of course, in Raku TIMTOWDI, so a for loop (or several) is perfectly fine.

Also, in this case I would really time how much the loop, or map, is going to take. For files that don't have many lines, race is not going to give you much, and it might even be slower, due to overhead. If your intention is to beat the clock a bit, IO::Handle.Supply is probably going to be a bit faster.

like image 32
jjmerelo Avatar answered Sep 28 '22 10:09

jjmerelo


Update: As recommended by Elizabeth Mattijsen it is more efficient use skip instead of tail.

for $file.IO.lines.skip(3).race

There is a tail routine you can use:

for $file.IO.lines.tail(*-3).race
{
  #do something, such as
  my ($a,$b)=.split(" ");
}
like image 124
LuVa Avatar answered Sep 28 '22 09:09

LuVa