· Offer Kaye > Perl > Reverse Paragraphs

Explanation of 'Reverse Paragraphs' Perl oneliner

Here is the code again: perl -00e'print reverse<>'
"reverse<>" - since reverse is in the list context provided by "print", it will return a list value consisting of the elements of <> (the input paragraphs) in the opposite order.
"print" - will print the list returned by "reverse". "-00" - generally, the command line switch "-0[digits]" specifies the input record separator ($/) as an octal number. Specifically, the special value 00 will cause Perl to slurp files in paragraph mode. Note that "paragraph" here means one or more consecutive lines matching /^$/ (i.e. a completly blank line) seperate the paragraphs from one another.

Usage Example

Given an input file called "example" with the contents:
a
b

c
d

Then running the following from the command line:
perl -00e'print reverse<>' example
will print to STDOUT the following four lines:
c
d
a
b

Note that there was no empty line between "d" and "a" because there was no empty line following "d".

Offer Kaye / To contact me, email me at: offer.kaye no spam at gmail dot com / Last Modified: Thu Jul 15 23:04:09 2004

Valid XHTML 1.0! Valid CSS!