#!/usr/bin/perl -w use strict; my %file; while(<>) { my $type = substr($_,0,3); defined($file{$type)) or open($file{$type}, ">$type.out") or die "Couldn't open $type.out: $!"; print $file{$type}; }