use strict; use warnings; my @order = qw( trap hair ); my %found; my $search = join '|', @order; while( ) { if( m/($search)/i ) { chomp; push @{$found{lc $1}}, $_; } } foreach my $wanted ( @order ) { if( exists $found{$wanted} ) { print "$_\n" for @{$found{$wanted}}; } } __DATA__ Transformers robots in disguise I whip my hair back and forth I will catch a dog with a trap #### I will catch a dog with a trap I whip my hair back and forth