#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %store; while () { next if /^\s*$/; chomp; s !//!/!g; s !:!!g; my @bits = split /\.|\//; my $key = ( join "}{", @bits); my $do_this = '$store{' . $key . '}++'; eval $do_this; } print Dumper(\%store); __DATA__ http://www.foo.com/this.html http://www.foo.com/index.html http://www.foo.com/that.html http://www.foo.com/cgi-bin/that.cgi http://www.foo.com/cgi-bin/user.cgi http://www.fred.com/index.html http://www.foo.org/index.html # output $VAR1 = { 'http' => { 'www' => { 'foo' => { 'org' => { 'index' => { 'html' => 1 } }, 'com' => { 'index' => { 'html' => 1 }, 'that' => { 'html' => 1 }, 'this' => { 'html' => 1 } } }, 'fred' => { 'com' => { 'index' => { 'html' => 1 } } } } } };