[ale] Perl quickie

Dylan Northrup docx at io.com
Fri Jan 16 11:32:49 EST 2004


A long time ago, (16.01.04), in a galaxy far, far away, Chris Fowler wrote:

:=I'm trying to remove all spaces in a string.
:=
:=        my $dir = $tag->{GENRE}."/".$info->{ARTIST}."/".$info->{ALBUM};
:=        my $dir =~ (s/\ /\_/g);
:=        print $dir."\n";
:=
:=What is the right way to do it?

plato> perl
$dir = '/foo/bar   baz/quux blah';
$dir =~ s/\s+/_/g;
print $dir . "\n";
^D
/foo/bar_baz/quux_blah

I don't know why you have the parentheses around the substitution.  I also
used '\s+' to collapse any instances of multiple spaces to a single '_'.
Another benefit of using '\s' is it will also get tabs, returns, etc. in
addition to plain old spaces.

-- 
Dylan Northrup <*> docx at io.com <*> http://www.io.com/~docx/
"Harder to work, harder to strive, hard to be glad to be alive, but it's 
 really worth it if you give it a try." -- Cowboy Mouth, 'Easy'



More information about the Ale mailing list