Velocity Reviews > Perl > Golf Q: \\$h{X} ?

# Golf Q: \\$h{X} ?

J Krugman
Guest
Posts: n/a

 04-04-2005

In an old FWP post I found online, there is a golfing construct I
can't figure out; this is all the post says

\\$h{X} is one less than ++\$h{X}

If it weren't for the leading backslash, the interpretation of "one
less" would be obvious and hardly worth remarking upon in such a
post. But given the leading backslash, I figure that the "one
less" is referring to "one fewer characters". Still, I can't figure
out what the whole thing means. Any help would be much appreciated.

TIA!

jill

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

Arndt Jonasson
Guest
Posts: n/a

 04-04-2005

J Krugman <(E-Mail Removed)> writes:
> In an old FWP post I found online, there is a golfing construct I
> can't figure out; this is all the post says
>
> \\$h{X} is one less than ++\$h{X}
>
> If it weren't for the leading backslash, the interpretation of "one
> less" would be obvious and hardly worth remarking upon in such a
> post. But given the leading backslash, I figure that the "one
> less" is referring to "one fewer characters". Still, I can't figure
> out what the whole thing means. Any help would be much appreciated.

That "one less" refers to the number of characters is my interpretation
too. I assume the idea is to make a hash table entry exist by referring
to it (I think that's what's called "autovivification"):

\\$h1{X};
++\$h2{X};

\$h0 = exists \$h0{X} ? "yes" : "no";
\$h1 = exists \$h1{X} ? "yes" : "no";
\$h2 = exists \$h2{X} ? "yes" : "no";

print "\$h0 \$h1 \$h2";

prints "no yes yes".

J Krugman
Guest
Posts: n/a

 04-04-2005
In <(E-Mail Removed)> Arndt Jonasson <(E-Mail Removed)> writes:

>J Krugman <(E-Mail Removed)> writes:
>> In an old FWP post I found online, there is a golfing construct I
>> can't figure out; this is all the post says
>>
>> \\$h{X} is one less than ++\$h{X}
>>
>> If it weren't for the leading backslash, the interpretation of "one
>> less" would be obvious and hardly worth remarking upon in such a
>> post. But given the leading backslash, I figure that the "one
>> less" is referring to "one fewer characters". Still, I can't figure
>> out what the whole thing means. Any help would be much appreciated.

>That "one less" refers to the number of characters is my interpretation
>too. I assume the idea is to make a hash table entry exist by referring
>to it (I think that's what's called "autovivification"):

> \\$h1{X};
> ++\$h2{X};

> \$h0 = exists \$h0{X} ? "yes" : "no";
> \$h1 = exists \$h1{X} ? "yes" : "no";
> \$h2 = exists \$h2{X} ? "yes" : "no";

> print "\$h0 \$h1 \$h2";

>prints "no yes yes".

Thanks!

jill

--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.