$string = "Good O'l Time"; $string =~ s/\ /_/g; $string =~ s/\'//g; print $string;
perldoc perlre perldoc perlretut
Try this,
use strict; use warnings; my $string = "Good O'l Time"; $string =~ y/ '/\_/d; print $string;
Regards,
Velusamy R.
Or rather: tr/ '/_ /;
I'd prefer this, actually:
for ($string) { tr/ '/_ /; print }
ayrnieu, OP's need
Good_Ol_Timebut your output shows
Good_O l_TimeAs per perlfunc - Perl builtin functions tutorial.
y///
The transliteration operator. Same as tr///.
Regards,
Velusamy R.
if you're trying to normalize a string so you can make a decent filename out of it- which i get the feeling here (slap me if i'm wrong..)
my $fus=qq|This Is some Funkah Lookin' & Zmellin' "straeyang"|; $fus=~s/\W/_/g; # now $fus equals This_Is_some_Funkah_Lookin____Zmellin_ __straeyang_ #great. super. let's clean that up $fus=~s/_+/_/g; # now $fup equals This_Is_some_Funkah_Lookin_Zmellin_straeyang_ #let's get rid of any _ in ends $fus=~s/^_+|_+$//g; print $fus; # prints This_Is_some_Funkah_Lookin_Zmellin_straeyang #maybe im tight and i don't want any uppercase $fus = lc($fus); #now $fus = this_is_some_funkah_lookin_zmellin_straeyang #anyway.. you get the idea
Maybe you want to get rid of funny chars? like non word chars? then use \W, that means *any* non word characters, as opposed to (\w which are 0-9, a-z, A-Z, _ anyway..)
Good post. Here's what I would do if all I wanted was your last result:
# note the lower-casing inline ($fus = lc $fus) =~ s/\W+/_/g; #all spans of 1 or more non-words become 1 '_' $fus =~ s/^_|_$//g; #at most one on each end, because of the above
perlmonks.org content © perlmonks.org and acid06, Anonymous Monk, ayrnieu, lamp, leocharre, radiantmatrix, Samy_rio
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03