#!/usr/bin/perl # LISTS... @list = (1,2,3,4); @list = reverse(@list); # result: @list = (4,3,2,1) # HASHES... # because of the way hashes flatten into lists sometimes, reverse can # also be used to invert a hash table, assuming the values are all unique. %myhash = (1 => 5, 2 => 10, 3 => 15, 4 => 20); %myhash = reverse(%myhash); # result: %myhash = (5=>1, 10=>2, 15=>3, 20=>4); # AND SCALARS... $x = "flinch"; $x = reverse( $x ); # result: $x = "hcnilf" # A ROUTINE... # here is a subroutine that will reverse lines as you type them, or # maybe you could use it on the command line if this file were saved as reverse.pl: # % cat myfile.txt | reverse.pl &reverse_cat(); sub reverse_cat() { my( $line ); while( $line = ) { chomp( $line ); $line = reverse( $line ); print "$line\n"; } }