Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How does this "JAPH" work?

by kcorj2244 (Novice)
on Sep 06, 2017 at 17:32 UTC ( [id://1198781]=perlquestion: print w/replies, xml ) Need Help??

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

I've been stumped on this one for a while:

$_ = "wftedskaebjgdpjgidbsmnjgc"; tr/a-z/oh, turtleneck Phrase Jar!/; print;
Prints : "Just another Perl Hacker,"

Replies are listed 'Best First'.
Re: How does this "JAPH" work?
by talexb (Chancellor) on Sep 06, 2017 at 17:41 UTC
    $_ = "wftedskaebjgdpjgidbsmnjgc"; tr/a-z/oh, turtleneck Phrase Jar!/; print; abcdefghijklmnopqrstuvwxyz | | | +- J +------------------ u ..

    I've expanded the alphabet (a-z) and aligned it with the phrase it's being replaced with, to show you how the first two characters map. The first w maps to the J in Jar, the next f maps to the u in 'turtleneck', and so forth. Simple! :)

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: How does this "JAPH" work?
by AnomalousMonk (Archbishop) on Sep 06, 2017 at 18:46 UTC

    talexb has shown how this simple substitution works. Note, however, that the output is not "Hacker" as you have it in the OP, but "hacker" (little-h vice big-H). To verify your understanding of what's going on, what simple change to the replacement string of the  tr/// operator would be necessary to make the output "Hacker"? (Of course, this simple change screws up another thing, but that's life... :)


    Give a man a fish:  <%-{-{-{-<

Re: How does this "JAPH" work?
by davido (Cardinal) on Sep 07, 2017 at 03:52 UTC

    There are three statements in this JAPH:

    1. $_ = "wftedskaebjgdpjgidbsmnjgc";
    2. tr/a-z/oh, turtleneck Phrase Jar!/;
    3. print;

    Step one, assign some encrypted text to $_; the "it" variable (Perl's default variable). See perlvar for an explanation of $_.

    Step two, transliterate. See perlop for an explanation of how tr/abc/def/ works, but the gist is that it walks through a string and changes any chcaracter found in the "abc" group to the character in the same position in the "def" list. Hence, if the string contains a "a", it would be replaced with a "d". The string that it acts upon by default is the one contained in $_.

    Step three, print the contents of $_. print prints the contents of $_ if there is no other argument passed to it.

    This is essentially a form of substitution cipher that leverages a couple of the terse shortcuts Perl offers to achieve a small degree of code illegibility.


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1198781]
Approved by talexb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found