Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Well, the comments describe what the code actually does, but I guess you're asking *why* it does what it does.

Let's take one particular statement and go over it--since they all have a similar structure, I think that'll help you to figure out the rest.

Let's take this one:

# to squish a string y sssscccc;
The comments provided say that this "squishes" a string. If you actually try this out in a little test program, you'll see exactly what it does:
$_ = "bookkeeper"; y sssscccc; print;
This prints "bokeper"--in other words, it gets rid of consecutive letters ("oo" => "o").

Why does it do that? Let's write the statement in a slightly different way:

tr///scccc;
This is the same statement, because:

"y" is a synonym for "tr";

and the "tr" operator can use any character to separate the SEARCHLIST from the REPLACEMENTLIST.

So in the obfuscated example, "tr" is "y", and the separator is "s". We can replace those to make it look more "normal", just as we did above.

Why all the "c"'s at the end, though? Well, we can take most of those off, too:

tr///sc;
And now we no longer have obfuscated code: this is just the regular "tr" operator, working as advertised.

The "c" tells "tr" to complement the SEARCHLIST; the SEARCHLIST is an empty string, so the complement is any character--so we're searching for any character. The "s" tells "tr" to "squish" the string-- from perlman:perlop:

If the /s modifier is specified, sequences of characters that were transliterated to the same character are squashed down to a single instance of the character.
In other words, "oo" => "o", and so on.

The final thing going on is that if the REPLACEMENTLIST is empty (as it is here), the SEARCHLIST is replicated (so the SEARCHLIST and REPLACEMENTLIST are the same).

So we're translating every character (the complement of an empty SEARCHLIST) into itself, *unless* there's a sequence of characters that translate to the same character, in which case we "squish" the sequence down to one instance of that character.

So, I hope this helps. Try to figure out the rest yourself, using some test programs and perlman:perlop.

And if I've made any mistakes, someone please correct them. :)


In reply to RE: RE: Tricks with tr/// by btrott
in thread Tricks with tr/// by japhy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (7)
    As of 2014-12-25 17:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (161 votes), past polls