faqts : Computers : Programming : Languages : Perl : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

Entry

Is there a way to copy some text from a string? IE: Copy all A-Z,a-z chars from - $string = "Hello."

Apr 18th, 2006 17:35
John Cassidy, Abs,


It's pretty straightforward. 

my $string = 'Hello, I must be going.'; 

# so we don't destroy the $string, we copy it. 
my $azcopy = $string; 

# I'll break this down, down below. 
$azcopy =~ s/[^a-z]//gi;

print qq(\$azcopy="$azcopy"\n);

There is a single statement version, that I don't like the look of: 

( $azcopy = $string ) =~ s/[^a-z]//gi;

For a more intuitive copy though this also kind of works: 

my $azcopy = join( '', $string =~ /([a-z])/ig );

or this: 

my $azcopy = join( '', split /[^a-z]/i, $string );