bugsbunny has asked for the wisdom of the Perl Monks concerning the following question:

could someone give me some simple text-scramble algorithm.. best if it is one-liner..:")
Just want to deface the text, so that a simple "sight" on it dont reveal what is it exactly..
tia

Title edit by tye

Replies are listed 'Best First'.
Re: scramble
by ChrisS (Monk) on Oct 03, 2003 at 14:46 UTC
    One way to do it:
    perl -MList::Util=shuffle -e "print shuffle split //, qq(This is a lin +e)" ilis iensTha
Re: scramble
by simonm (Vicar) on Oct 03, 2003 at 15:50 UTC
    It's fairly common to rot-13 text for such purposes: $text =~ tr[a-zA-Z][m-za-nM-ZA-N];

    As a bonus, running the same function a second time will return the original text.

    Update: The 'M's and 'N's in the above are swapped, as davido points out below. Oh well, that's what I get for posting before I've drunk my coffee...

      tr[a-zA-Z][m-za-nM-ZA-N];

      That isn't a rot13, it's a mistake.

      Rot13 is tr[a-zA-Z][n-za-mN-ZA-M];

      And here it is as a one liner that Rot13's all files specified on the command line, and saves their original as filename.old:

      perl -pi.old -e "tr/a-zA-Z/n-za-mN-ZA-M/" filename


      Dave


      "If I had my life to do over again, I'd be a plumber." -- Albert Einstein
Re: scramble
by ChrisS (Monk) on Oct 03, 2003 at 15:46 UTC
    I guess I'm a bit bored, today, because I came up with another way:
    perl -e "print sort {rand > .5} split //, qq(More Text)"
    Update: That probably should have been:
    perl -e "print sort {rand <=> .5} split //, qq(More Text)"
      sort {rand > .5}

      That's a bad way to scramble text, because it uses a bad method of performing a sort. The documentation makes the following point:

      The comparison function is required to behave. If it returns inconsistent results (sometimes saying $x[1] is less than $x[2] and sometimes saying the opposite, for example) the results are not well-defined.

      The usual definitions of not well-defined include dumps core and fails to terminate. It might work, but only by luck.

        Thanks for the cautionary note!

        I wonder if the inconsistency might cause the sort to continue making swaps indefinitely, thus causing the failure to terminate that you mentioned?

        Of course, my simple test case did just fine, but that probably was dumb luck.

Re: scramble text simply
by bugsbunny (Scribe) on Oct 06, 2003 at 13:45 UTC
    thanx alot