Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Counting the Extensions of the Directory

by Mago (Parson)
on Dec 13, 2005 at 19:51 UTC ( #516401=sourcecode: print w/replies, xml ) Need Help??
Category: Miscellaneous
Author/Contact Info Marco Lima <mago at>
Description: Counting the Extensions of the Archives in Current Directory.
#!/usr/bin/perl -w
use strict;

my %extensions;

while (<*>) {
    if (-f && /\.(\W+)$/) {

foreach $key (keys %extensions) {
    print "$key -> $extensions{$key}";



perl -e 'while(<*>){if(-f && /\.(\W+)$/){$e{$1}++}}print "$_ -> $e{$_}
+\n" for keys %e'
Replies are listed 'Best First'.
Re: Counting the Extensions of the Directory
by merlyn (Sage) on Dec 13, 2005 at 21:39 UTC
    I'm not sure what an extension is, because it's apparently some file with a period followed by non-alphanums at the end of the name. That's pretty rare. Did you mean \w+ instead of \W+?

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Ok ! Yes, you are certain, with \w+ is very better.

      perl -e 'while(<*>){if(-f && /\.(\w+)$/){$e{$1}++}}print "$_ -> $e{$_} +\n" for keys %e'


Re: Counting the Extensions of the Directory
by EdwardG (Vicar) on Dec 14, 2005 at 10:39 UTC
    E:\tmp>ls 1gfJaJTeoh CFG1584.tmp CFG1587.tmp EScan ExchangePerflog_8484fa31f2d96a21cfcccd43.dat Perflib_Perfdata_784.dat SCP1D28.tmp SCPA62.tmp VBE WINWORD.log add_service_authentication_columns.sql add_service_authentication_columns.sql.bak ch01.pdf create_export_tables-2.sql create_export_tables-2.sql.bak en-only.rws.swp jusched.log msohtml msohtml1 perldoc_BDeparse_T439ed4fb_d50b1.txt perldoc_perlfunc_T439eb5b6_bc83c.txt plugtmp tmp158D.exe tmp158D.tmp tmp158E.tmp tmp1592.tmp tmp180E.tmp tmp1811.tmp ~DF67F.tmp ~DF68C.tmp ~DF723C.tmp ~DF850E.tmp ~DFDDFC.tmp ~WRD0828.doc ~WRF2409.tmp ~WRS0003.tmp ~WRS0004.tmp E:\tmp>ls | grep -oE "\.\w+$" | sort -u .bak .dat .doc .exe .log .pdf .pl .sql .swp .tmp .txt .xls E:\tmp>

    (GNU grep 2.5.1)


      You are not counting the extensions ?

      IBIZA desentol /tarifa1/users/desentol/mlima/dados/input > ls | grep - +oE "\.\w+$" | sort -u grep: illegal option -- o usage: grep [-E | -F] [-c|-l|-q] [-insvxbhwy] [-p parasep] -e pattern_ +list... [-f pattern_file...] [file...] usage: grep [-E | -F] [-c|-l|-q] [-insvxbhwy] [-p parasep] [-e pattern +_list...] -f pattern_file... [file...] usage: grep [-E | -F] [-c|-l|-q] [-insvxbhwy] [-p parasep] pattern [fi +le...]


        GNU grep 2.5.1 has the -o option to output only the matched text. Your grep doesn't.

        As for counting...

        E:\tmp>ls | grep -oE "\.\w+$" | sort -u | wc -l 12


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://516401]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2021-10-21 19:17 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (83 votes). Check out past polls.