Path: utzoo!attcan!uunet!samsung!think!mintaka!ogicse!schaefer From: schaefer@ogicse.ogi.edu (Barton E. Schaefer) Newsgroups: comp.lang.perl Subject: Ordering one array by another Message-ID: <8251@ogicse.ogi.edu> Date: 27 Mar 90 01:28:29 GMT Organization: Oregon Graduate Institute (formerly OGC), Beaverton, OR Lines: 41 I have a sorted list of keywords, and a file (well, stdin actually, but no matter) consisting of lines each of which contains at most one of the keywords. There may be keywords not in the file, and lines with zero keywords (which are ignored), but no pair of lines both contain the same keyword. What I want to do is reorder the input lines to match the order in the list of keywords. So what's the fastest way? What I have right now goes like this: @lines = ; # Whomp it in # Never mind how I got the sorted keywords, they're in @keys foreach $k (@keys) { # For every keyword $i = $[; # Reset the index foreach $l (@lines) { # For every line if ($l =~ /\b$k\b/) { # If it matches print splice(@lines,$i,1); # Remove and print it last; # Done with this pass } $i++; # Increment index } } The idea being that the splice shortens the search for each succeeding pass through the lines. Of course Randal would point out that it could be: grep(($k = $_, $i = 0, $found = 0, grep((!$found && ++$i && /\b$k\b/ && (print(splice(@lines,$i-1,1)), ++$found)), @lines)), @keys); I'm wondering if there isn't some trick to do this faster than either of these, perhaps by mixing grep and foreach, or using an associative array. Suggestions? -- Bart Schaefer "EARTH: Surrender IMMEDIATELY or we PICKLE DAN QUAYLE" "THPPFT!" schaefer@cse.ogi.edu (used to be cse.ogc.edu)