tr/// and unicode

tr/// and unicode

am 08.09.2011 10:34:44 von Matthias Leopold

hi,

perl -e '$_ =3D "äö"; tr/"ä","ö"/"ae","oe"/; print $_."\n";'

expected result: aeoe
actual result: aeae

why?

thx
matthias


--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: tr/// and unicode

am 08.09.2011 10:53:30 von Uri Guttman

>>>>> "ML" == Matthias Leopold writes:

ML> perl -e '$_ =3D "äö"; tr/"ä","ö"/"ae","oe"/; print $_."\n";'

not that i do unicode much but that tr/// is wrong. it takes a single
string in each part, not lists of "" strings. and it can't replace 1
char with 2. you need s/// for that. this works (as i said, i am an
ascii bigot! :)

$_ =3D "äö";

s/ä/ae/ ;
s/ö/ao/ ;

print "$_\n" ;

aeao

uri

--=20
Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com =
--
------------ Perl Developer Recruiting and Placement Services -----------=
--
----- Perl Code Review, Architecture, Development, Training, Support -----=
--

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: tr/// and unicode

am 08.09.2011 11:13:02 von Matthias Leopold

Am 2011-09-08 10:53, schrieb Uri Guttman:
>>>>>> "ML" == Matthias Leopold writes:
>
> ML> perl -e '$_ =3D "äö"; tr/"ä","ö"/"ae","oe"/; print $_."=
\n";'
>
> not that i do unicode much but that tr/// is wrong. it takes a single
> string in each part, not lists of "" strings.

perl -e '$_ =3D "ao"; tr/"a","o"/"b","p"/; print $_."\n";'

works as (i) expected -> "bp"

from perlop: tr/SEARCHLIST/REPLACEMENTLIST/cds

> and it can't replace 1
> char with 2.

perl -e '$_ =3D "ä"; tr/"ä"/"ae"/; print $_."\n";'

that replaces 1 char with 2

> you need s/// for that. this works (as i said, i am an
> ascii bigot! :)

actually, the code i'm trying to replace uses s///. i wanted to use=20
something similar to str_replace() in php which uses two arrays for=20
search and replace (in the php version of the very same task in my=20
project). but i admit that i'm not too familiar with tr/// and even=20
unicode (although living in "Österreich"/Austria ;-))


matthias





--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: tr/// and unicode

am 08.09.2011 11:49:18 von jeffpang

SGksCgpZb3UgbWlnaHQgd2FudCBzLy8vIG9wZXJhdG9yLiBGb3IgZXhhbXBs ZSwKCiQgcGVybCAt
bGUgJyRzdHI9IuS4reaWhyI7JHN0cj1+cy/kuK3mlocvQ2hpbmVzZS87cHJp bnQgJHN0cicKQ2hp
bmVzZQoKJCBlbnZ8Z3JlcCBMQU5HCkxBTkc9ZW5fVVMuVVRGLTgKCgowOCDR gdC10L3RgtGP0LHR
gNGPIDIwMTEsIDEyOjM1INC+0YIgTWF0dGhpYXMgTGVvcG9sZCA8bWF0dGhp YXNAYWljLmF0PjoK
PiBoaSwKPiAKPiBwZXJsIC1lICckXyA9ICLDpMO2IjsgdHIvIsOkIiwiw7Yi LyJhZSIsIm9lIi87
IHByaW50ICRfLiJcbiI7Jwo+IAo+IGV4cGVjdGVkIHJlc3VsdDogYWVvZQo+ IGFjdHVhbCByZXN1
bHQ6IGFlYWUKPiAKPiB3aHk/Cj4gCj4gdGh4Cj4gbWF0dGhpYXMKPiAKPiAt LQo+IFRvIHVuc3Vi
c2NyaWJlLCBlLW1haWw6IGJlZ2lubmVycy11bnN1YnNjcmliZUBwZXJsLm9y Zwo+IEZvciBhZGRp
dGlvbmFsIGNvbW1hbmRzLCBlLW1haWw6IGJlZ2lubmVycy1oZWxwQHBlcmwu b3JnCj4gaHR0cDov
L2xlYXJuLnBlcmwub3JnLwo+IAo+IAotLQogIEplZmYgUGFuZwogIGplZmZw YW5nQG1haWwucnUK

Re: tr/// and unicode

am 08.09.2011 12:22:19 von jwkrahn

Matthias Leopold wrote:
>
> perl -e '$_ = "ao"; tr/"a","o"/"b","p"/; print $_."\n";'
>
> works as (i) expected -> "bp"
>
> from perlop: tr/SEARCHLIST/REPLACEMENTLIST/cds

LIST in SEARCHLIST and REPLACEMENTLIST refers to a list of characters.

If we rearrange your example above:

perl -e '
$_ = "ao";

tr{"a","o"}
{"b","p"};

print $_."\n";
'

You get: the character " is replaced with the character "
and the character a is replaced with the character b
and the character " is replaced with the character "
and the character , is replaced with the character ,
and the character " is replaced with the character "
and the character o is replaced with the character p
and the character " is replaced with the character "

And if you have multiple characters the same in the SEARCHLIST then only
the first one will have any effect in the REPLACEMENTLIST.


So that could be written more simply and efficiently as:

tr/ao/bp/;




John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: tr/// and unicode

am 08.09.2011 12:29:27 von Matthias Leopold

Am 2011-09-08 12:22, schrieb John W. Krahn:
> Matthias Leopold wrote:
>>
>> perl -e '$_ = "ao"; tr/"a","o"/"b","p"/; print $_."\n";'
>>
>> works as (i) expected -> "bp"
>>
>> from perlop: tr/SEARCHLIST/REPLACEMENTLIST/cds
>
> LIST in SEARCHLIST and REPLACEMENTLIST refers to a list of characters.
>
> If we rearrange your example above:
>
> perl -e '
> $_ = "ao";
>
> tr{"a","o"}
> {"b","p"};
>
> print $_."\n";
> '
>
> You get: the character " is replaced with the character "
> and the character a is replaced with the character b
> and the character " is replaced with the character "
> and the character , is replaced with the character ,
> and the character " is replaced with the character "
> and the character o is replaced with the character p
> and the character " is replaced with the character "
>
> And if you have multiple characters the same in the SEARCHLIST then only
> the first one will have any effect in the REPLACEMENTLIST.
>
>
> So that could be written more simply and efficiently as:
>
> tr/ao/bp/;
>
>
>
>
> John

thx for all replies
by now i have realized, that i misunderstood tr///. i'll stick with s///.

matthias


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: tr/// and unicode

am 08.09.2011 19:02:50 von Uri Guttman

>>>>> "ML" == Matthias Leopold writes:

ML> Am 2011-09-08 10:53, schrieb Uri Guttman:
>>>>>>> "ML" == Matthias Leopold writes:
>>=20
ML> perl -e '$_ =3D "äö"; tr/"ä","ö"/"ae","oe"/; print $_."\n";'
>>=20
>> not that i do unicode much but that tr/// is wrong. it takes a single
>> string in each part, not lists of "" strings.

ML> perl -e '$_ =3D "ao"; tr/"a","o"/"b","p"/; print $_."\n";'

ML> works as (i) expected -> "bp"

that is one char to one char which is how tr/// works. it can't replace
single chars with multiple chars like you want.

and the "" parts are still wrong. they actually don't make any
difference here but they are not correct syntax for tr///

ML> from perlop: tr/SEARCHLIST/REPLACEMENTLIST/cds

and nowhere does it show comma lists of "" strings inside the //. please
read the docs again and learn to use tr/// correctly.


>> and it can't replace 1
>> char with 2.

ML> perl -e '$_ =3D "ä"; tr/"ä"/"ae"/; print $_."\n";'

ML> that replaces 1 char with 2

show output that it does. it should not work like you think it does.

ML> actually, the code i'm trying to replace uses s///. i wanted to use
ML> something similar to str_replace() in php which uses two arrays for
ML> search and replace (in the php version of the very same task in my
ML> project). but i admit that i'm not too familiar with tr/// and even
ML> unicode (although living in "Österreich"/Austria ;-))

tr/// will not do what you want. you have to accept that and also use
the correct syntax.


--=20
Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com =
--
------------ Perl Developer Recruiting and Placement Services -----------=
--
----- Perl Code Review, Architecture, Development, Training, Support -----=
--

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: tr/// and unicode

am 09.09.2011 16:53:17 von Mike McClain

On Thu, Sep 08, 2011 at 11:13:02AM +0200, Matthias Leopold wrote:

> actually, the code i'm trying to replace uses s///. i wanted to use
> something similar to str_replace() in php which uses two arrays for
> search and replace (in the php version of the very same task in my
> project). but i admit that i'm not too familiar with tr/// and even
> unicode (although living in "?sterreich"/Austria ;-))

You might look at:
substr EXPR,OFFSET,LENGTH,REPLACEMENT
as a replacement for str_replace().

HTH,
Mike
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/