[1+1=2]

OneAndOneIs2

« Adding complexity to reduce complexityPerls of Visdom »

Thu, Mar 15, 2012

[Icon][Icon]Privacy & social media

• Post categories: Omni, My Life, Programming

There's been yet another complaint doing the rounds about things you put onto Facebook/Twitter/Google+ not being kept private.

I personally think anyone who puts anything onto any such site that they don't consider "public domain" is simply insane, so I can't say I was interested enough in whatever the latest outcry is about to actually read it.

But it did remind me of a joke I saw once where somebody said that, to keep their messges private, they encrypted them with ROT13 at least twice, sometimes four or even six times!

ROT13, for those who don't know, moves the letters of the alphabet 13 characters along - so A<->N, B<->O, C<->P etc. So doing it twice takes you full-circle and leaves a message unaltered.

But that, in turn, reminded me of an old UserFriendly strip which was itself ROT13'd. And I'm trying to get more practice at writing perl one-liners, so I had a quick go at writing a ROT13 , as it was lunchtime.

It turned out to be laughably simple. The script would be:

while (my $input = <STDIN>){
    $input =~ tr#[a-m][n-z]#[n-z][a-m]#;
    print $input;
    }

But there's no reason not to just use $_ instead of assigning a variable when turning it into a one-liner.

perl -e 'while (<STDIN>){ $_ =~ tr#[a-m][n-z]#[n-z][a-m]#; print $_; }'

And then, of course, since $_ is used by default, it can be dropped altogether:

perl -e 'while (<STDIN>){ tr#[a-m][n-z]#[n-z][a-m]#; print; }'

And that's readable enough - whilst you have input from STDIN, translate the letters appropriately & print them.

But referring to famous perl one-liners, I was able to refine it a bit further.

Firstly, <STDIN> is implied by just <> - I didn't realise that until now. And then calling perl with -p implies a while(<>) loop AND prints the output for you. So you can actually shrink the whole thing down to just:

perl -pe 'tr#[a-m][n-z]#[n-z][a-m]#;'

Not bad for a complete ROT13 program, I think.

Feel free to use this to protect your own tweets & status updates as many times as you like ;)


4 comments

pdh
Comment from: pdh [Visitor]
One simpler

[a-m][n-z] confuses me... that says to me.. "grab everything in this range[a-m] or in this range [n-z]"... where the entire range is just a-z!

So it can become.

perl -pe 'tr/[a-z]/[n-z][a-m]/;'

... Also the square brackets are redundant in this context, so you can write it like so.

perl -pe 'tr/a-z/n-za-m/;'
16/03/12 @ 19:22
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
Just [a-z] doesn't work for me, not sure why. But it DOES work if you skip the []s altogether. Cool! :)
16/03/12 @ 19:48
dmaestro12
Comment from: dmaestro12 [Visitor]
the square brackets are NOT metacharacters in your example, so it is actually translating a '[' to a '[' ;-) and a ']' to a ']'. Therefore, they had to be paired similarly or, better, eliminated.
An example from perlop uses the following:

"tr[A-Z][a-z]"

It might be easy to forget that those '[]' are bracketing quotes, rather than metacharacters if you are used to s///.
19/03/12 @ 14:29
oneandoneis2
Comment from: oneandoneis2 [Member] · http://geekblog.oneandoneis2.org/
Aha! That explains everything! Cheers!
19/03/12 @ 15:00
 

[Links][icon] My links

[Icon][Icon]About Me

[Icon][Icon]About this blog

[Icon][Icon]My /. profile

[Icon][Icon]My Wishlist

[Icon]MyCommerce

[FSF Associate Member]


November 2014
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Search

User tools

XML Feeds

eXTReMe Tracker

Valid XHTML 1.0 Transitional

Valid CSS!

[Valid RSS feed]

blog engine