A problem with tr
am 11.10.2007 00:34:44 von dn.perl
Code being run under ActiveState Perl (latest download) :
my $str1 = "11,11,2222,3" ;
$str1 =~ tr/,// ;
print "$str1 \n" ;
my $str2 = "11,11,2222,3" ;
$str2 =~ tr/,/Z/ ;
print "$str2 \n" ;
===
Output :
11,11,2222,3
11Z11Z2222Z3
So I am able to replace comma with 'Z' , but I am not able to make it
disappear using tr.
Why is that so?
Please advise. Thanks in advance.
Re: A problem with tr
am 11.10.2007 01:14:08 von Martien Verbruggen
On Wed, 10 Oct 2007 22:34:44 -0000,
dn.perl@gmail.com wrote:
> my $str1 = "11,11,2222,3" ;
> $str1 =~ tr/,// ;
> print "$str1 \n" ;
>
> my $str2 = "11,11,2222,3" ;
> $str2 =~ tr/,/Z/ ;
> print "$str2 \n" ;
>===
>
> Output :
> 11,11,2222,3
> 11Z11Z2222Z3
>
> So I am able to replace comma with 'Z' , but I am not able to make it
> disappear using tr.
> Why is that so?
From the documentation of tr/// in the perlop document [1]:
Options:
c Complement the SEARCHLIST.
d Delete found but unreplaced characters.
s Squash duplicate replaced characters.
If the "/c" modifier is specified, the SEARCHLIST character set
is complemented. If the "/d" modifier is specified, any char-
acters specified by SEARCHLIST not found in REPLACEMENTLIST are
deleted.
But also, and this, I assume, is the source of your confusion:
If the "/d" modifier is used, the REPLACEMENTLIST is always
interpreted exactly as specified. Otherwise, if the REPLACE-
MENTLIST is shorter than the SEARCHLIST, the final character is
replicated till it is long enough. If the REPLACEMENTLIST is
empty, the SEARCHLIST is replicated. This latter is useful for
counting characters in a class or for squashing character
sequences in a class.
I suspect that what you're looking for is:
$str1 =~ tr/,//d;
Martien
[1] You can get to the documentation by using the man command on a
Unix-like OS, perldoc on almost any OS, and from HTML pages on some
distibutions. If you don't already know this, please take some time to
get familiar with Perl's documentation and your system's way of making
it available to you.
--
|
Martien Verbruggen | There are only 10 types of people in the
| world; those who understand binary and those
| who don't.
Re: A problem with tr
am 11.10.2007 01:52:45 von Jim Gibson
In article <1192055684.268534.139670@g4g2000hsf.googlegroups.com>,
<"dn.perl@gmail.com"> wrote:
> Code being run under ActiveState Perl (latest download) :
> my $str1 = "11,11,2222,3" ;
> $str1 =~ tr/,// ;
> print "$str1 \n" ;
>
> my $str2 = "11,11,2222,3" ;
> $str2 =~ tr/,/Z/ ;
> print "$str2 \n" ;
> ===
>
> Output :
> 11,11,2222,3
> 11Z11Z2222Z3
>
> So I am able to replace comma with 'Z' , but I am not able to make it
> disappear using tr.
> Why is that so?
Because it is implemented that way. See 'perldoc perlop' and search for
'SEARCHLIST'.
Short answer: Use tr/,//d;
The rule is that if the replacement list is empty and the d modifier is
not specified, the search list is also used as the replacement list. In
that case, no replacements are done, but you get a count of the
characters in the string.
--
Jim Gibson
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com