in reply to Removing File Extensions

my @files = qw( one.zip twotwo.doc three3.ppt ); my (@only_names, @only_ext); for (@files) { my ($name, $ext) = $_ =~ m!\A(.*)\.([^.]+)\z!s; push( @only_names, $name ); push( @only_ext, $ext ); }

Replies are listed 'Best First'.
Re: Re: Removing File Extensions
by sgifford (Prior) on May 01, 2004 at 20:35 UTC
    This fails if the file has no extension. Probably a simple test for a match failure would fix this:
    my($name,$ext); ($name, $ext) = m!\A(.*)\.([^.]+)\z!s or $name = $_,$ext='';
Re: Re: Removing File Extensions
by BalochDude (Initiate) on May 01, 2004 at 19:38 UTC
    Q1: If i only want to get the name. Is the following code correct.
    Replace
    my ($name, $ext) = $_ =~ m!\A(.*)\.([^.]+)\z!s;
    with
    my ($name) = $_ =~ m!\A(.*)\z!s;


    Q2: Could you please explain this line
    ($name, $ext) = $_ =~ m!\A(.*)\.([^.]+)\z!s;
    Not in detail , i just want some idea.
    I am really impressed with the way i got a reply. it was very quick.

    jdporter - added code formatting