Re: problems with tr///;
by suaveant (Parson) on Sep 25, 2001 at 22:31 UTC
|
$script =~ tr/\\//d;
- Ant
- Some of my best work - Fish Dinner
| [reply] [d/l] |
|
I think you're right. It's odd, but none of the books or docs that I have show any flags to run tr/// with...
Thanks for your help on an insanely moronic question <you guys here are always nice, even when dumb questions are asked> Thanks again!
Justin
| [reply] |
|
Check out perldoc perlop for the options
to tr. As it appears our site docs are still a bit old,
here is an excerpt from a Perl5.6.0 dist:
c Complement the SEARCHLIST.
d Delete found but unreplaced characters.
s Squash duplicate replaced characters.
U Translate to/from UTF-8.
C Translate to/from 8-bit char (octet).
jeffa | [reply] |
|
none of the books or docs that I have show any flags to run tr/// with
perldoc perlop
| [reply] |
|
| [reply] |
|
| [reply] |
|
My copy of the Camel certainly mentions all the tr///; switches. But, I'll admit when I first started Perl I would buy these books off the Street Vendors in NYC for $5. I got the Dummies book and one about Perl for Windows NT Servers. Both of them were barely worth even the little cash I shelled out, and left out major details.
The O'Reilly books are popular for a reason.
| [reply] |
Re: problems with tr///;
by broquaint (Abbot) on Sep 25, 2001 at 22:38 UTC
|
The reason it won't work is because by default tr, transliterates, or changes, character for character between the search list to the replacement list. However if you stick a 'd' on the end it will delete everything in the search list that doesn't have a corresponding replacement e.g
$var =~ tr/\\//d; # \ deleted, nothing to replace
$var =~ tr/\\/X/d; # changed \ into X
As someone is bound to say RTFM (man perlop).
HTH
broquaint | [reply] [d/l] |
Re: problems with tr///;
by thunders (Priest) on Sep 25, 2001 at 23:37 UTC
|
I go for substitution in cases like this
in which case
<code size=+1>$script =~ s/\\//g;</code>
should do what your looking for.
I often use tr to count stuff.
as in
<code size=+1>$x = $somestring =~ tr/0-9/0-9/;
#find the number of digits in $somestring and put that value in $x</code>
| [reply] |
Re: problems with tr///;
by Sigmund (Pilgrim) on Sep 26, 2001 at 13:14 UTC
|
why use the translate operator?
couldn't you use the substitution operator, like:
$script =~ s/\\//g;
(the g meaning "globally", as to say "match each \ in string".
otherwise you could even use the numeric ascii code to specify a "\".
SiG
perl -le 's ssSss.s sSsSiss.s s$sSss.s s.$s\107ss.print'
| [reply] |
|
| [reply] |
|
use Benchmark;
$var = '\a\\aa\\\aaa\\\\aaaa'x100;
print "s: ";
timethis(100000, '$var =~ s/\\\//g'); # 3 \ for benchmark
print "tr: ";
timethis(100000, '$var =~ tr/\\\//d');
and here's the results
s: timethis 100000: 1 wallclock secs ( 0.51 usr + 0.00 sys = 0.51
+CPU) @ 195694.72/s (n=100000)
tr: timethis 100000: 2 wallclock secs ( 1.68 usr + 0.00 sys = 1.68
+CPU) @ 59453.03/s (n=100000)
You can play with $var and see the effect on the timings.
Maybe tr used to be faster, but that's no longer the case.
<soapbox>
It's really beside the point. The point is the right tool for the job. In this case the right tool is the substitution operator. If you wanted to make multiple single character changes in the string then tr is what you want. Note the difference between:
$var =~ tr /ab/cd/;
# and
$var =~ s /ab/cd/g;
</soapbox>
Have fun,
Carl Forde | [reply] [d/l] [select] |
|
|
Re: problems with tr///;
by Anonymous Monk on Sep 25, 2001 at 23:09 UTC
|
try s/\\//g;
or better s#\##g;
OEzzi | [reply] |
|
s#\##g doesn't work. The substitution is incomplete. Tack another # on the end and see what happens:
$_ = '\\##hello world';
s#\##g#;
print;
You still need to escape the "\" regardless of what seperator you use. The # seperator is useful for "/"
Simon Flack ($code or die)
$,=reverse'"ro_';s,$,\$,;s,$,lc ref sub{},e;$,
=~y'_"' ';eval"die";print $_,lc substr$@,0,3;
| [reply] [d/l] |
Re: problems with tr///;
by DaveMWithnall (Initiate) on Sep 27, 2001 at 14:29 UTC
|
you could try :
$string =~ s/\\//g;
which will do it. | [reply] |
|
| [reply] |