my %unique; if ( $have_a_line_to_print ) { $unique{$have_a_line_to_print} = 1; } # # much later # for( sort keys %unique ) { print; }