TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG:18790] quick way to strip spaces...



Luke Francl wrote:
> On Thu, 15 Jun 2000, Troy Johnson wrote:
> > #!/usr/bin/perl -w
> > opendir D, ".";
> > for (readdir D)
> > {
> >       $oldname = $_;
> >       s/[ \[\]{}<>(),~]/_/g;
> >       s/\.MP3/.mp3/g;
> >       rename $oldname, $_;
> >       chmod 0655, $_;
> > }
> As an aside, I really admire Perl's power and maturity as a language. But
> I can't stand the syntax! I mean, $_, yeah that's intuitive.

I must agree, unfortunately. I like having a default when I am in a
hurry, but I usually regret using it if I have to look at the code
later.

Fortunately, you don't have to use it:

#!/usr/bin/perl -w
opendir DIRECTORY, ".";
foreach $filename (readdir DIRECTORY)
{
      $newfilename = $filename;
      $newfilename =~ s/[ \[\]{}<>(),~]/_/g;
      $newfilename =~ s/\.MP3/.mp3/g;
      rename $filename, $newfilename;
      chmod 0655, $newfilename;
}
close DIRECTORY;

But it doesn't make the regular expressions any easier to read. ;-)

-- 
Troy Johnson   mailto:john1536@tc.umn.edu   http://umn.edu/~john1536/
"...The Bill of Rights is a literal and absolute document. The First
Amendment doesn't say you have a right to speak out unless the
government has a 'compelling interest' in censoring the Internet. The
Second Amendment doesn't say you have the right to keep and bear arms
until some madman plants a bomb. The Fourth Amendment doesn't say you
have the right to be secure from search and seizure unless some FBI
agent thinks you fit the profile of a terrorist. The government has no
right to interfere with any of these freedoms under any circumstances."
	-- Harry Browne, 1996 USA presidential candidate, Libertarian Party