zigdon / xkcd-Bucket

Bucket is the channel bot for #xkcd
http://wiki.xkcd.com/irc/Bucket
121 stars 31 forks source link

Variable resolution order #63

Open ElectronicRU opened 9 years ago

ElectronicRU commented 9 years ago

The form like $var$who or similar forms containing actual variables or different pseudo-variables will resolve incorrectly if the rightmost one is expanded first by the code into something with an alphanumeric start. Building a hash of all substitutions and substituting at once might be in order to fix this. (Discovered when adding "random webiste" factoids, id didn't quite work out gluing stuff together.)

zigdon commented 9 years ago

One way around this might be to do a preprocessing, where we change s/\$(\w+)/\${$1}/g. That way, replacing the later var won't append its contents to the previous one.

dgw commented 8 years ago

One of my users just ran into this with a factoid that uses multiple instances of $digit both before and after $nonzero. Since all of the $digits get replaced first, the code then looks for variables such as $nonzero2 and fails.

I tried to fix it by manually putting {} around the problem variable, and it does work—except that the braces are not removed during the replacement step. Would it be worthwhile for me to submit a PR to do so, and make braces usable to manually fix troublesome factoids in the mean time before this issue is tackled? (No promises on timeline; am out of town for quite a while.)

dgw commented 5 years ago

In the long time since posting that, I discovered at some point that ${varname} works just fine. Presumably I was trying to do {$varname} when I stated that the braces weren't removed.

So, this issue has an effective workaround.