how to substiture a letter in a string using perl?

P

Paul

hi, there,

I have a date format string, like $mydate='Wed Feb 1 17:33:20 2006';

I dont want to have the space and : in my string, so I use

$mydate =~ tr/[ ,:]/a/; to get a new string as
$mydate='WedaFebaa1a17a33a28a2006';

The question is how can I get rid all the "a" in this string? or how to
get $mydate='WedFeb11733202006' from $mydate='Wed Feb 1 17:33:20
2006'?


thanks.
 
X

Xicheng

Paul said:
hi, there,

I have a date format string, like $mydate='Wed Feb 1 17:33:20 2006';

I dont want to have the space and : in my string, so I use

$mydate =~ tr/[ ,:]/a/; to get a new string as
$mydate='WedaFebaa1a17a33a28a2006';

The question is how can I get rid all the "a" in this string? or how to
get $mydate='WedFeb11733202006' from $mydate='Wed Feb 1 17:33:20
2006'?

$mydate =~ tr/ ://d;
 
A

A. Sinan Unur

I have a date format string, like $mydate='Wed Feb 1 17:33:20 2006';

I dont want to have the space and : in my string, so I use

$mydate =~ tr/[ ,:]/a/; to get a new string as
$mydate='WedaFebaa1a17a33a28a2006';

The question is how can I get rid all the "a" in this string? or how
to get $mydate='WedFeb11733202006' from $mydate='Wed Feb 1 17:33:20
2006'?

Please feel free to read the documentation.

D:\Home\asu1\UseNet\clpmisc> perldoc -f tr
tr/// The transliteration operator. Same as "y///". See perlop.

Then find tr in perldoc perlop:

#!/usr/bin/perl

use strict;
use warnings;

my $mydate = 'Wed Feb 1 17:33:20 2006';

$mydate =~ tr/ ://d;

print "$mydate\n";

__END__
 
D

DJ Stunks

Paul said:
hi, there,

I have a date format string, like $mydate='Wed Feb 1 17:33:20 2006';

I dont want to have the space and : in my string, so I use

$mydate =~ tr/[ ,:]/a/; to get a new string as
$mydate='WedaFebaa1a17a33a28a2006';

The question is how can I get rid all the "a" in this string? or how to
get $mydate='WedFeb11733202006' from $mydate='Wed Feb 1 17:33:20
2006'?


thanks.

oh, that's easy:

$mydate='Wed Feb 1 17:33:20 2006';
$mydate =~ tr/[ ,:]/a/;
$mydate =~ tr/a/b/; # no more a's

no charge, glad to help :)

-jp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top