my $prev_key = ""; my %HOH; while ( a line read from file ) { ...get the current key .. code that you have already if ( $current_key ne $prev_key) { if ($prev_key ne "" ) #don't dump first time through! { ...dump the %HOH #this is the previous result } $prev_key = $current_key; ...%HOH=(); #reset! } ...processing of the current_key } ...dump the %HOH ... #the last record...