use IO::File; ... my %fd; my %files = ( 'somestring' => 'somefilename', 'someother' => 'someother', ); my @items = ( 'somestring.this.content.will.go.into.somefilename', 'someother.this.will.go.into.someother', 'notthere.this.will.generate.a.warning', ); foreach @items { my @array = split(/\./); if (exists($files{$array[0]})) { $fd{$array[0]} ||= new IO::File "> $files{$array[0]}" or die "Couldn't open $files{$array[0]}: $!"; $fd{$array[0]}->print; } else { warn "No filename defined for $array[0]"; } } $fd{$_}->close for keys %fd;