I'm implementing string parity functions using perl's String:
arity
as the base to work from. I'm assuming that the ruby code is working
as it was designed to, but I want to find out what I need to do to
make it work like the perl example. I just posted a shorter, easier
to test version. The perl version outputs a string correctly set to
even parity. The ruby code doesn't. I've tried quotes, no quotes,
double quotes, eval, beating my head in the wall, all of that. It's
something else I am missing.
Chris
I think it's due to multiple interpolations - first in double quotish
context and then as a pattern for tr, where \ and - are the only real
metacharacters.
Looks like you need to do something like use \\\55 in place of \55 (a
- character) and \\\134 in place of \134 (a \ character)
Ick.
Program and output below
#!/usr/bin/env ruby
str = "()*+,-./09AZ[_]abcpqrstuvwxyz\376"
p str
str.tr!("\0-\377","\0\201\202\3\204\5\6\207\210\11\12\213\14\215\216
\17\220\21\22\223\24\225\226\27\30\231\232\33\234\35\36\237\240\41\42
\243\44\245\246\47\50\251\252\53\254\\\55\56\2570\261\2623\26456\267
\2709\72\273\74\275\276\77\300AB\303D\305\306GH\311\312K\314MN\317P
\321\322S\324UV\327\330YZ\333\\\134\335\336_\140\341\342c\344ef\347
\350ij\353l\355\356o\360qr\363t\365\366wx\371\372\173\374\175\176\377
\0\201\202\3\204\5\6\207\210\11\12\213\14\215\216\17\220\21\22\223\24
\225\226\27\30\231\232\33\234\35\36\237\240\41\42\243\44\245\246\47\50
\251\252\53\254\\\55\56\2570\261\2623\26456\267\2709\72\273\74\275\276
\77\300AB\303D\305\306GH\311\312K\314MN\317P\321\322S\324UV\327\330YZ
\333\\\134\335\336_\140\341\342c\344ef\347\350ij\353l\355\356o\360qr
\363t\365\366wx\371\372\173\374\175\176\377")
p str
output:
"()*+,-./09AZ[_]abcpqrstuvwxyz\376"
"(\251\252+\254-.\25709AZ\333_\335\341\342c\360qr\363t\365\366wx\371
\372~"
Hope this helps,
Mike
--
Mike Stok <
[email protected]>
http://www.stok.ca/~mike/
The "`Stok' disclaimers" apply.