[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

perl tr and ruby String#tr

snacktime

8/12/2006 8:57:00 AM

This, in perl:
$str =~ tr/\0\200/\200\0/;

Produces a different result then this:

str.tr!("\0\200","\200\0")

How do I do this in ruby?

13 Answers

Robert Klemme

8/12/2006 10:33:00 AM

0

snacktime <snacktime@gmail.com> wrote:
> This, in perl:
> $str =~ tr/\0\200/\200\0/;
>
> Produces a different result then this:
>
> str.tr!("\0\200","\200\0")
>
> How do I do this in ruby?

Input data? Output data? Expected results?

robert

snacktime

8/12/2006 6:40:00 PM

0

my $even_bits = "\0";
my $odd_bits = "\200";
foreach (0 .. 7) {
$even_bits .= $odd_bits;
$odd_bits = $even_bits;
$odd_bits =~ tr/\0\200/\200\0/;
}
print unpack("c*","$odd_bits");

Output:
-12800-1280-128-12800-128-1280-12800-1280-128-1280-12800-128-12800-1280-128-12800-128-1280-12800-128-12800-1280-128-1280-12800-1280-128-12800-
128-1280-12800-1280-128-1280-12800-128-12800-1280-128-1280-12800-1280-128-12800-128-1280-12800-128-12800-1280-128-12800-128-1280-12800-1280-12
8-1280-12800-128-12800-1280-128-12800-128-1280-12800-128-12800-1280-128-1280-12800-1280-128-12800-128-1280-12800-128-12800-1280-128-12800-128-
1280-12800-1280-128-1280-12800-128-12800-1280-128-1280-12800-1280-128-12800-128-1280-12800-1280-128-1280-12800-128-12800-1280-128-12800-128-12
80-12800-128-12800-1280-128-1280-12800-1280-128-12800-128-1280-12800-128

even_bits = "\0"
odd_bits = "\200"
8.times do
even_bits << odd_bits
odd_bits = even_bits
odd_bits.tr!("\0\200","\200\0")
end

print odd_bits.unpack('c*')

Output:
0-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-
1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-12
80-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280
-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1
280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-1280-128

Jano Svitok

8/12/2006 7:06:00 PM

0

On 8/12/06, snacktime <snacktime@gmail.com> wrote:

> even_bits = "\0"
> odd_bits = "\200"
> 8.times do
> even_bits << odd_bits
> odd_bits = even_bits
> odd_bits.tr!("\0\200","\200\0")
> end
>
> print odd_bits.unpack('c*')

- odd_bits = even_bits
+ odd_bits = even_bits.clone

= assigns reference to the object in this case

snacktime

8/12/2006 7:14:00 PM

0

On 8/12/06, Jan Svitok <jan.svitok@gmail.com> wrote:
> On 8/12/06, snacktime <snacktime@gmail.com> wrote:
>
> > even_bits = "\0"
> > odd_bits = "\200"
> > 8.times do
> > even_bits << odd_bits
> > odd_bits = even_bits
> > odd_bits.tr!("\0\200","\200\0")
> > end
> >
> > print odd_bits.unpack('c*')
>
> - odd_bits = even_bits
> + odd_bits = even_bits.clone
>
> = assigns reference to the object in this case
>
>
Ok now I feel stupid:) Thanks for pointing that out.

Jano Svitok

8/12/2006 7:49:00 PM

0

On 8/12/06, snacktime <snacktime@gmail.com> wrote:
> On 8/12/06, Jan Svitok <jan.svitok@gmail.com> wrote:
> > On 8/12/06, snacktime <snacktime@gmail.com> wrote:
> >
> > > even_bits = "\0"
> > > odd_bits = "\200"
> > > 8.times do
> > > even_bits << odd_bits
> > > odd_bits = even_bits
> > > odd_bits.tr!("\0\200","\200\0")
> > > end
> > >
> > > print odd_bits.unpack('c*')
> >
> > - odd_bits = even_bits
> > + odd_bits = even_bits.clone
> >
> > = assigns reference to the object in this case
> >
> >
> Ok now I feel stupid:) Thanks for pointing that out.

Well, I looked at it for quite a long time before I realized what's
going on: at first I replaced your strings with 'A's and 'B's as these
are easier to recognize, then I tried 'AA-AB-BA-BB'.tr(...) and both
versions were ok. So it wasn't a tr problem. Soon after that the
inspiration came... ;-)

You're welcome.

J.

snacktime

8/13/2006 2:32:00 AM

0

Ok almost there one more question on using tr. If the to_string is a
variable, what to do? In perl you would use eval because the
transliteration for the from_string and to_string are done at compile
time. Following is the perl code that produces the correct result,
and the ruby code which I can't get to produce the same output. Any
ideas on how to do this correctly in ruby?


$even_parity = '\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\44245\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\3
27\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\207210\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\2
61\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\34
4ef\347\350ij\353l\355\356o\360qr\363t\365\366wx\371\372\173\374\175\176\377';

eval <<EDQ;

sub setEvenParity {
my(\@s) = \@_;
foreach (\@s) {
tr/\\0-\\377/$even_parity/;
}
wantarray ? \@s : join '', \@s;
}

EDQ
die $@ if $@;

$out = setEvenParity('s');
print $out;


@even_parity = '\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\44245\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\3
27\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\207210\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\2
61\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\34
4ef\347\350ij\353l\355\356o\360qr\363t\365\366wx\371\372\173\374\175\176\377';

eval <<EDQ

def setEvenParity(str)
str.tr("\\0-\\377",@even_parity)
end

EDQ

out = setEvenParity('s')
print out

Mike Stok

8/13/2006 3:06:00 AM

0


On 12-Aug-06, at 10:32 PM, snacktime wrote:

> Ok almost there one more question on using tr. If the to_string is a
> variable, what to do? In perl you would use eval because the
> transliteration for the from_string and to_string are done at compile
> time. Following is the perl code that produces the correct result,
> and the ruby code which I can't get to produce the same output. Any
> ideas on how to do this correctly in ruby?
>
>
> $even_parity = '\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\3
> 27\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\2
> 61\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\34
> 4ef\347\350ij\353l\355\356o\360qr\363t\365\366wx\371\372\173\374\175
> \176\377';
>
> eval <<EDQ;
>
> sub setEvenParity {
> my(\@s) = \@_;
> foreach (\@s) {
> tr/\\0-\\377/$even_parity/;
> }
> wantarray ? \@s : join '', \@s;
> }
>
> EDQ
> die $@ if $@;
>
> $out = setEvenParity('s');
> print $out;
>
>
> @even_parity = '\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\3
> 27\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\2
> 61\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\34
> 4ef\347\350ij\353l\355\356o\360qr\363t\365\366wx\371\372\173\374\175
> \176\377';
>
> eval <<EDQ
>
> def setEvenParity(str)
> str.tr("\\0-\\377",@even_parity)
> end
>
> EDQ
>
> out = setEvenParity('s')
> print out

Have you tried the seemingly simple approach like this, which avoids
eval:

ratdog:~ mike$ irb --prompt simple
>> @upper_case = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
=> "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
>> def uc(s)
>> s.tr('abcdefghijklmnopqrstuvwxyz', @upper_case)
>> end
=> nil
>> uc('banana')
=> "BANANA"
>> @upper_case = 'abcdefghijklmnopqrstuvwxyz'
=> "abcdefghijklmnopqrstuvwxyz"
>> uc('banana')
=> "banana"

Maybe I am misunderstanding what you want to do...

Hope this helps,

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.





snacktime

8/13/2006 3:20:00 AM

0

> Have you tried the seemingly simple approach like this, which avoids
> eval:
>
> ratdog:~ mike$ irb --prompt simple
> >> @upper_case = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
> => "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
> >> def uc(s)
> >> s.tr('abcdefghijklmnopqrstuvwxyz', @upper_case)
> >> end
> => nil
> >> uc('banana')
> => "BANANA"
> >> @upper_case = 'abcdefghijklmnopqrstuvwxyz'
> => "abcdefghijklmnopqrstuvwxyz"
> >> uc('banana')
> => "banana"

Yes I tried it without eval also, but it won't produce the same output
as the perl code. I've tried every incantation I can think of but no
luck. I'm checking my results by comparing them to the output of
perl's String::Parity. The perl code I posted is basically an
abbreviation of what String::Parity does when setting even parity.
It's the last bit in this damn module I'm working on also:) If I get
this figured out I'll be done.

snacktime

8/13/2006 3:30:00 AM

0

Here is a shortened version. the ruby and perl code do not produce
the same output. I want the ruby version to produce the same output
as the perl version.

In perl:

$str = "s";
$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\324
UV\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\6207\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\25
70\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\342
c\344ef\347\350ij\353l\355\356o\360qr\363t\365\366wx\371\372\173\374\175\176\377/;

In ruby:

's'.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\324
UV\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\6207\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\25
70\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\342
c\344ef\347\350ij\353l\355\356o\360qr\363t\365\366wx\371\372\173\374\175\176\377")

Mike Stok

8/13/2006 3:35:00 AM

0


On 12-Aug-06, at 11:19 PM, snacktime wrote:

>> Have you tried the seemingly simple approach like this, which avoids
>> eval:
>>
>> ratdog:~ mike$ irb --prompt simple
>> >> @upper_case = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>> => "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
>> >> def uc(s)
>> >> s.tr('abcdefghijklmnopqrstuvwxyz', @upper_case)
>> >> end
>> => nil
>> >> uc('banana')
>> => "BANANA"
>> >> @upper_case = 'abcdefghijklmnopqrstuvwxyz'
>> => "abcdefghijklmnopqrstuvwxyz"
>> >> uc('banana')
>> => "banana"
>
> Yes I tried it without eval also, but it won't produce the same output
> as the perl code. I've tried every incantation I can think of but no
> luck. I'm checking my results by comparing them to the output of
> perl's String::Parity. The perl code I posted is basically an
> abbreviation of what String::Parity does when setting even parity.
> It's the last bit in this damn module I'm working on also:) If I get
> this figured out I'll be done.

Can you give me a hint as to what doesn't work? For a simple upper
case subset of your code (using double quotes) I see

ratdog:~ mike$ irb --prompt simple
>> @even_parity = "AB\303D\305\306GH\311\312K\314MN\317P\321\322S
\324UV\327\330YZ"
=> "AB\303D\305\306GH\311\312K\314MN\317P\321\322S\324UV\327\330YZ"
>> @even_parity.length
=> 26
>> def setEvenParity(s)
>> s.tr('A-Z', @even_parity)
>> end
=> nil
>> setEvenParity('ABACAB')
=> "ABA\303AB"

seems pretty reasonable - can you show a simple example of what
doesn't behave as you want?

Hmm, thinking about it, when you do the Ruby eval, are uou missing
quotes around @even_parity?

Mike

--

Mike Stok <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.