Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!dali.cs.montana.edu!uakari.primate.wisc.edu!sdd.hp.com!ucsd!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: Needed: a pointer for a perl compare script Message-ID: <1990Nov30.194813.17716@iwarp.intel.com> Date: 30 Nov 90 19:48:13 GMT References: <12020@milton.u.washington.edu> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 58 In-Reply-To: djo7613@hardy.u.washington.edu (Dick O'Connor) In article <12020@milton.u.washington.edu>, djo7613@hardy (Dick O'Connor) writes: | My program reads two files of differing format which are sorted by a unique | 5-character label. When two labels match, a new record is written, with | info from file A (moved around a bit) written to the "left" and info from | file B (again, reformatted a little) written to the "right". Where a | given record from file A or B has no counterpart, the same new record is | written, with blanks on the "side" without counterpart information. | | I know this is simple; it's a short program now. But there's something | I'm just not seeing that blocks my conversion to perl. A pointer to a | suggested construct would be wonderful...I'm happy to work out the details. Hmm... (warning... untested code follows)... If I could fit all of file A and B into memory (my preferred tactic), I'd do something like this: open(A,"Afile"); while() { chop; ($label,$rest) = unpack("a5a*",$_); $a{$label} = $rest; $both{$label}++; } close(A); open(B,"Bfile"); while() { chop; ($label,$rest) = unpack("a5a*",$_); $b{$label} = $rest; $both{$label}++; } close(B); for (sort keys both) { $left = defined($a{$_}) ? $a{$_} : "left default"; $right = defined($b{$_}) ? $b{$_} : "right default"; print "$_ $left $right\n"; } This'd probably take some massaging, but I hope you get the general idea. If they don't both fit into memory, you will have to do some juggling to read from a or b depending on which of the current labels are lower. This solution here is much simpler (and elegant :-) by comparison. | BTW I've been away for two weeks; did I miss The Announcement about The Book? The only announcement you may have missed is that Larry and I are working intensely to incorporate the review comments and finish up the final draft so that we can still make the Usenix deadline. print "Just another Perl [book] hacker," -- /=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: "Intel: putting the 'backward' in 'backward compatible'..."====/