Gossamer Forum
Home : General : Perl Programming :

Can this be simplified?

Quote Reply
Can this be simplified?
 
$Title =~ tr/A-Z/a-z/; #this converts to small caps
$Title =~ s/[0-9]//g; #this removes all numbers
$Title =~ s/[&$\#.!',"(*)\:;]//g; #this removes some other characters
$Title =~ s/ /-/g; #this replaces blank with hyphen

I want all the numbers and characters gone, spaces replaced with hyphen (and possibly 2-3 series of hyphens replaced with one hyphen) and title converted to lower case.

Thanks
Quote Reply
Re: [socrates] Can this be simplified? In reply to
You can probably simplify it a little, but probably not a great deal.

Code:
$Title =~ s/[\d\W]//g; # Get rid of everything other than a-z and underscores.
$Title = lc($Title); # Lowercase it.
$Title =~ s/ /-/g; # Spaces to hyphens.
Quote Reply
Re: [Hargreaves] Can this be simplified? In reply to
Thanks but I don't want to get rid of underscores or spaces (the first one) and after substituting for hyphens for spaces and underscores, I would like to get rid of multiple hyphens (if possible).
Quote Reply
Re: [socrates] Can this be simplified? In reply to
The first one doesn't get rid of underscores....but it does get rid of spaces.

To get rid of multiple hypens you could just do:

Code:
$Title =~ s/--+/-/g;

THat may give an error due to the position of the hyphens so if it does just escape the first two, like \-\-