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";