The auto-increment operator

eggzeck on 2006-10-31T09:29:58

As I read the perlop manpage (a while back, as everyone should), I find that the auto-increment operator "++" has a bit of built-in magic. Basically: If you use a variable in string context since it was set (other rules apply, read the perlop manpage for more information), the increment is done as a string.

Following these rules, and of course with some creativity, I came up with a way to print my nick "eggzeck"; of course this is for fun, and I'm sharing it for fun/educational purposes. Well, I don't know how much you'll actually learn from this, so it's mostly for fun.

Here's what I came up with:

$nick = join("", map(++($holder = $_), qw(d f f y d b j)));

Pretty cool? The $nick variable will now contain the string "eggzeck". :-)


Re:

Aristotle on 2006-10-31T11:51:16

my $nick = join "", map { my $x = "a"; ++$x for 1 .. $_; $x } 3568, 460444;

Semi-golfie version

Yanick on 2006-10-31T13:35:00

$"="";$nick="@{[map++$_,@==dffydbj=~/./g]}";
print $nick;