CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to remove unmatched string in tr///

 



Ram.bm
Novice

Dec 21, 2013, 1:16 AM

Post #1 of 5 (1339 views)
How to remove unmatched string in tr/// Can't Post

my$str = 'aa bbb c dd eee ff mmmm rr' ;
$str =~tr/abcd/1234\t/;
print $str,"\n";

output:
11 222 3 44 eee ff mmmm rr

how to remove this unmatched string.

Code

Code


	

(This post was edited by Ram.bm on Dec 21, 2013, 1:23 AM)


BillKSmith
Veteran

Dec 21, 2013, 6:46 AM

Post #2 of 5 (1329 views)
Re: [Ram.bm] How to remove unmatched string in tr/// [In reply to] Can't Post

Do you intend to replace all characters except a-d with tabs in addition to the changes you did make? If so, use two tr's.

Code
use strict; 
use warnings;
my $str = 'aa bbb c dd eee ff mmmm rr' ;
$str =~tr/a-d/|/c;
$str =~tr/a-d/1-4/;
print $str,"\n";



OUTPUT:

Code
11|222|3|44|||||||||||||||


Note: I used the character "|" rather than "\t" to display the effect.
Good Luck,
Bill


2teez
Novice

Dec 21, 2013, 11:57 AM

Post #3 of 5 (1321 views)
Re: [Ram.bm] How to remove unmatched string in tr/// [In reply to] Can't Post

Or you can do like this:

Code
use strict;  
use warnings;

my $str = 'aa bbb c dd eee ff mmmm rr' ;
$str =~tr/e-z//d; # remove the unwanted letters
$str =~tr/a-d/1-4/; # do the transliteration
print $str, $/;


The above solution is a modification of BillKSmith Solution using d instead of c


Ram.bm
Novice

Dec 22, 2013, 9:18 PM

Post #4 of 5 (1288 views)
Re: [2teez] How to remove unmatched string in tr/// [In reply to] Can't Post

you answer is easy to understand.What is the use of $/

?


2teez
Novice

Dec 23, 2013, 9:06 AM

Post #5 of 5 (1274 views)
Re: [Ram.bm] How to remove unmatched string in tr/// [In reply to] Can't Post


Quote
What is the use of $/

?

This is the input record separator, which is newline by default. Use

Code
perldoc -v $/

for more information.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives