my $previous = ""; # skip $item variable # 'for' just looks funny in this use foreach (@list) { # regex once instead of twice # no extra work shuffling $previous back and forth until needed if (m/^(.+)\: .+/ and $1 ne $previous) { $previous = $1; # take the sleep hit on the first item to save time later # (maybe even 5 seconds worth if you have a lot of items) sleep(5); } print $_; }