[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [FWP] comm Golf



> From: Alistair.McGlinchy@marks-and-spencer.com
> [mailto:Alistair.McGlinchy@marks-and-spencer.com]
> Sent: Thursday, May 04, 2000 14:44
> To: fwp@technofile.org
> Subject: RE: [FWP] comm Golf

...

> Nathan Torkington suggested
> >  perl -e'open A,shift;$h{$_}=1 while<A>;$h{$_}|=2
> while<>;map{print"\t"x($h{$_}-1),$_}sort keys%h'
> >  perl -e'$h{$_}|=2-@ARGV while<>;print"\t"x($h{$_}-1),$_ for sort
keys%h'
> >  perl -ne'$h{$_}|=2-@ARGV;END{print"\t"x($h{$_}-1),$_ for sort
keys%h}'
>
> As much as I like the techniques used here, Nathan's versions don't
print
> the same output as as Tim's. For eg, Nathan's doesn't deal with
duplicate
> entries in the one of the source files, nor does it print the output
in the
> same order as the input.

Yes, it does.  comm(1) requires sorted files, and so does the method you
post below.

But there is no arguing with the uniqueness issue.  Only one of
replicated entries will be printed.

> I can't see how to use a hash and still preserve the correct order of
both
> input files. Here's my current best at 131. But it seems extremely
beatable
> with three "print"s and two "shift"s:
>
> perl -e'open B,pop;open A,pop;@a=<A>;while(<B>){print shift@a
> while($a[0]lt$_&&$a[0]);$_=($a[0]eq$_)?"\t\t".shift@a:"\t$_";print}'

A couple of niggles:

  perl -e'open B,pop;@ARGV=pop;@a=<>;while(<B>){print shift@a
  while@a&&$a[0]lt$_;print$a[0]eq$_?"\t\t".shift@a:"\t$_"}'

--
Larry Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com



==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe