Thursday, July 10, 2008

Flip-flop operator

I have been using the flip-flop operator in Perl for ages, equating it with sed and awk line addressing. However a question just came up that asked how we process from a specific line until end-of-file. In sed and awk the last line of the file is denoted by $, but that does not work in Perl for obvious syntax reasons.

I had to think about that: the solution is to use eof(). So, to list a file from line 20 we can use:

while (<>)
print if 20..eof


When I signed on to Perlmonks this morning I got:

You have 24 votes left today.
You gained 16 experience points. They really complement your eyes.
Your new level imbues you with super-powers! Check out Level 13: Curate for the details!
You have 991 points until level 14 - Priest.