in reply to Removing File Extensions

This is a solved problem, so use the provided solution. This should work on any system. File::Basename is part of the core.
use File::Basename; my @files = ("one.zip","twotwo.doc","three3.ppt"); my @onlyNames; my @onlyExt; foreach my $file (@files) { my ($name, $path, $suffix) = fileparse( $file, qr/\.[^.]+$/ ); $suffix =~ s/\.//; push @onlyNames, $name; push @onlyExt, $suffix; }

Update: Fixed typo. (Thanks, Not_a_Number!)

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Replies are listed 'Best First'.
Re^2: Removing File Extensions
by tkil (Monk) on May 02, 2004 at 02:36 UTC

    I applaud your use of a prepackaged module, but I personally think it's a bit overkill in this situation.

    my @files = qw( foo.zip twotwo.doc three.one.four.ppt four ); my ( @only_names, @only_exts ); foreach ( @files ) { if ( m/^ (.*?) (?:\.([^.]+))? $/x ) { push @only_names, $1; push @only_exts, $2; # might be undef } else { warn "couldn't parse '$!'"; } } # only for debugging output foreach ( @only_exts ) { $_ = 'undef' unless defined $_ } print "names = [ @only_names ]\n", "exts = [ @only_exts ]\n";

    Which produced:

    names = [ foo twotwo three.one.four four ] exts = [ zip doc ppt undef ]

    I guess I feel that, since your solution even with a standard module also requires crafting a regex, I'd just as soon write a regex for the whole thing. *shrug*

    My main concerns with my solution would be whether the regex is efficient enough — the non-greedy filename part worries me a bit — and the usual "well, you might be able to craft such a regex, but the average programmer..." complaints.

      The point here isn't that either solution would require a regex or not. The goal is to teach someone that the issues one generally runs into have been run into many times before. There is often a wheel that's round enough out there.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        The goal is to teach someone that the issues one generally runs into have been run into many times before. There is often a wheel that's round enough out there.

        I guess our difference in opinion is where draw the line between "simple enough to do 'by hand'" versus "ok, this is hard, let's see if someone else has already done it better".

        I'm not against modules, but I am against unnecessary modules. In this case, as I said, I found the use of a module overkill: instead of having a single regex that did a fairly simple task (and I could comment that regex further, but I didn't think it needed it), I now need to read and understand the documentation for the module. (And, in some cases, install it, make sure it's up to date, etc; File::Basename is a standard module, so that isn't an issue here.)

        I once had a conversation with someone who thought a language (or, at least, a library / API) to generate regexes would be a handy thing to have. I disagreed, seeing as REs are already their own language; learning another one on top of the actual regex seemed unnecessary.