in reply to Hash ref and file extensions

This code should do what you need:

open(F, '<', './files.txt') or die "open: $!"; while ($filename = <F>) { chomp($filename); $dot = rindex($filename, '.'); if ($dot > -1) { $extension = substr($filename, $dot+1); $extensions{$extension}++; } else { $extensions{'_without_extension'}++; } } close(F); while (($key, $value) = each %extensions) { print "$key -> $value\n"; }

Ciao, Valerio

update: thanks liz!

Replies are listed 'Best First'.
Re: Re: Hash ref and file extensions
by liz (Monsignor) on Aug 16, 2003 at 12:50 UTC
    One small nit. I would replace this code:
    if ($dot > -1) { $extension = substr($filename, $dot+1); $extensions{$extension}++; } else { $extensions{'_without_extension'}++; }
    by:
    $extensions{$dot == -1 ? '' : substr( $filename,$dot )}++;
    for two reasons:
    1. it's more compact
    2. it allows you to differentiate between filenames without extension (no . found) and with an empty extension (a . at the end).
    Liz