DEV Community


Discussion on: Daily Challenge #29 - Xs and Os

vimmer9 profile image
Damir Franusic

Perl sorcery 😁. This syntax always turns me inside out hehe. Nice job 👍

yzhernand profile image
Yozen Hernandez • Edited

Sure, but this one is pretty easy to demystify, luckily: tr/set1/set2/ just replaces characters in set1 with those in set2 (positionally), and returns the number of characters replaced/deleted. So in this code, he just compares the number of X's replaced with the number of O's replaced, and includes the lower-case variants too.

The local($_) is just there to let him make the code more brief. $_ is the "default input/pattern-searching space", so its like the default argument that tr will search. But it's also a global, so this lets you make a local copy and set it to be the first argument to the function.

If he didn't do that, it might look like this:

sub xo { my $xos = shift; ($xos =~ tr/xX//) == ($xos =~ tr/oO//) }

So just a bit more verbose.

Thread Thread
alvaromontoro profile image
Alvaro Montoro

Thanks for a great explanation :)