Here's a cute little program that will turn a file of text upside-down (sort of):
#!/usr/bin/perl use strict; use warnings; my @lines; while(<>){ push @lines, $_; } while(@lines){ print flip_line(pop @lines); } sub flip_line {rev_string(join '', map flip_char($_), split //, shift)} sub rev_string {scalar reverse shift} sub flip_char {$_= lc shift; tr/'ahbbmfnnjpdrvutwqgye'/'eyqgwtuvrdpjnnfmbbha'/; $_}
For example, the file
some text can be flipped over using this script
becomes
fdijcs siyf buisn jano paddilt aq uec fxaf awos
Flipping the text back over can be approximated by something like
./umop-apisdn.pl < test.txt | ./umop-apisdn.pl
This is approximate because of case changes and because the mapping function I use doesn't invert cleanly (since v and u both man to n, for example).
Note: this idea is not originally mine, I just wrote this implementation. Check out http://poignantguide.net/ruby/chapter-7.html for the inspiration.
~dewey

Replies are listed 'Best First'.
Re: Turning text umop-apisdn
by jwkrahn (Abbot) on Mar 27, 2007 at 09:36 UTC
    Why are you using tr/// to change a single quote to a single quote -- twice? Why split and join the line? Why not just:
    for ( reverse <> ) { chomp; ( my $line = reverse lc ) =~ tr/ahbbmfnnjpdrvutwqgye/eyqgwtuvrdpjn +nfmbbha/; print "$line\n"; }
      Thanks for the clean-up. I didn't have a good understanding of all the different things reverse can do (in particular, the
      for ( reverse <> ) {
      usage is new to me). Splitting and joining... oh, that was left-over from when I thought I needed to split up the string in order to reverse it (again, I just don't know what reverse does, I'd better hit perldoc) and I just spaced on getting rid of those steps.
      Not sure that you mean by using tr/// twice...
      ~dewey

        He means that you do not need to quote the arguments to tr:

        tr/ahbbmfnnjpdrvutwqgye/eyqgwtuvrdpjnnfmbbha/

        Good Day,
            Dean