Path: utzoo!attcan!uunet!decwrl!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: Ordering one array by another Message-ID: <1990Mar27.161630.16724@iwarp.intel.com> Date: 27 Mar 90 16:16:30 GMT References: <8251@ogicse.ogi.edu> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 39 In-Reply-To: schaefer@ogicse.ogi.edu (Barton E. Schaefer) In article <8251@ogicse.ogi.edu>, schaefer@ogicse (Barton E. Schaefer) writes: | 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 | } | } Too much newfangled technology. Do it easy... $_ = "\n" . join("",); # slurrrrpp... for $k (@keys) { s/(\n)(.*\b$k\b.*\n)/print($2),$1/eg; # don't discard the previous newline "$1" } # at this point, $_ has "\n" followed by anything left... s/^\n//; print; @p=split(//,",rekcah lreP rehtona tsuJ");p:print pop(p);goto p if @p; -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/