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
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.
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(" ");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With