@JAPH[5]

Hi everyone, I'm back. School is crazy. I hope to keep writing blog posts, but they'll probably be infrequent. Here's some music:


Sadly, this is the only JAPH of mine I have left to share; we use C++ exclusively at school, so I haven't written anything new in Perl lately. Let's take a look:

@a='u';map{$x=push@a,chr(ord(@a[$x-1])+$_)}(
-43,42,-1,-18,-65,79,-1,-6,12,-2,-13,-21,-48
,82,-13,-69,76,-11,7,3,-8,15,-13,-57);for (0
..12){$_*=2;@a[$_,$_+1]=@a[$_+1,$_]}print@a;

Lots of numbers. 24, by my count, which is pretty close to that 25 character string we're so familiar with now. We can probably assume, then, that each number will be transformed to an ASCII value and printed. With that in mind, here's the properly formatted code:

@a = 'u';
map {$x = push @a, chr(ord(@a[$x - 1]) + $_)}
   (-43,42,-1,-18,-65,79,-1,-6,12,-2,-13,-21,
    -48,82,-13,-69,76,-11,7,3,-8,15,-13,-57);
for (0..12) {
    $_ *= 2;
    @a[$_, $_ + 1] = @a[$_ + 1, $_];
}
print @a;

The heart of this JAPH is the map function. In the expression block, we see that $x is being assigned to the result of pushing a new value onto @a. push?, as you might have guessed, simply adds the new value to the end of the array, and the documentation tells us it returns the new number of elements in the array. @a[$x - 1] seems to be calling the last element (not the penultimate element!), but in the first iteration, $x hasn't been initialized yet! That means its value is 0, so how can calling @a[-1] be valid? As it turns out, this is a handy feature of Perl; a negative array index means, "start at the end and count backwards." Since the array starts with only one element, any index, positive or negative, will return 'u'.

Now we can see what the significance of the numbers in the array are: they are the difference in ASCII value between the last element of the array and the new element we want to add. So the next character added to the array will be 'u' - 43 = 'J', then 'J' + 42 = 't', then 't' - 1 = 's'...
These letters should look very familiar. The final step is to arrange them properly, which is done with a for loop. The loop skips every other element, and swaps adjacent elements. I'll admit there's probably a cooler way to do this (with regex, for example), but my regex is pretty rusty at the moment. I'll update it once I figure out how. :)

Leave a Reply

Theme: Esquire by Matthew Buchanan.