Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: Just another JAPH

by kelan (Deacon)
on Sep 19, 2002 at 20:39 UTC ( #199294=note: print w/replies, xml ) Need Help??

in reply to Just another JAPH

Wow, this was a good one. I stared at it blankly for 10 minutes before deciding to whip out the ol' -MO=Deparse. thelenm++

Spoiler below...

Let's start in the middle.

q=qr!J& $s[l?P^h%t:r] @e(afk)nOrbadc/oge; } #u0t4h8ex,=
Owing to the single-quote operator, q{}, this is actually building a string, using = as delimiters. It creates a very ugly looking string.

Next we see this, attached to the end of our ugly string:

=~m"\C|0xaf3d!until$a+=$b; # secret key: "g
A regex, delimited by ". The /g modifier on the end means we're matching this as many times as we can. Now notice the very first item in the regex: \C. This matches "a single C char" according to perlre. This will always match, meaning everything after the | is just filler. So we build a string and match on it. Big deal. Well, working backward through the line, just before the ugly string we see:
So we perform a character by character match on this string and then assign the result to the %= hash. But what is being assigned? Since assigning to a hash puts us in list context, the match returns a list of matches. And since it matches every character, we're really just returning our ugly string split into characters, as if we'd done split('',ugly_string).

Now think about what the hash %= contains. The keys are every other character from our ugly string. The 1st, 3rd, 5th, etc. The odd number characters are keys. So the even numbered characters become values of those keys. Example:

$={'q'} eq 'r' $={'!'} eq 'J' $={'&'} eq ' ' $={'$'} eq 's' etc

Next we sort %=:

This flattens %= back in to a list and sorts it. This really gives us a sorted list of the characters from the ugly string. Now here comes the tricky part:
print @={our_sorted_list_of_chars}
That kind of looks like an assignment, but remember that @ in front of an array or hash name is the way to define a slice. So do we have an array or hash named =? Yes! Remember, we assigned our unsorted list of letters to it above. In fact, the way to define a hash slice is just so: @hash{'key5','key6','key7'}. So this last bit takes a slice from %= using as keys, all of the characters from our ugly string. Half of those letters actually exist as keys in the hash (the odd letters), and the other half do not exist as keys (the even letters, they were made into values). Within the slice, the letters that are not keys will return undef list items, and the keys that are found return their values. Those values are the even numbered letters from the string, all of them. And they'll be in order of the sorted order of their keys. You guessed it: when the keys are sorted, their values spell out "Just another Perl hacker,", which is the list returned from the slice, and that is what gets printed.


Yak it up with Fullscreen ChatBox

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://199294]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2020-10-27 05:31 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (256 votes). Check out past polls.