Inspired by a recent SoPW, how short can you write a diff of two simple csv files that displays the missing keys from the two files prefixed by the fileno they were missing from?

Some test data:

P:\test>type t1.csv 1,the,rest 2,the,rest 3,the,rest 4,the,rest 5,the,rest P:\test>type t2.csv 2,the,rest 4,the,rest 6,the,rest 8,the,rest

And my best that at 114 strokes can surely be well beaten?

Rules: Command, including all switches count; filenames don't.

# 23456789 123456789 123456789 123456789 123456789 123456789 123456789 + 123456789 123456789 123456789 123456789 1234 perl -anF, -le"$h{$F[0]}=1;eof&&last}{$h{(split',',$_)[0]}--while<>;$h +{$_}&&print $h{$_}<0?'1:':'2:',$_ for keys%h" t1.csv t2.csv 1:8 1:6 2:1 2:3 2:5

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re: [GOLF] Perl csv diff
by truedfx (Monk) on Nov 03, 2005 at 13:25 UTC
    Here's a quick one in 84 (updated from 86 because I had a pointless !! before eof) (unless I miscounted) -- relying on $^F defaulting to 2:
    #--------1---------2---------3---------4---------5---------6---------7 +---------8-------- #234567890123456789012345678901234567890123456789012345678901234567890 +123456789012345678 perl -anF, -le'$h{$F[0]}|=$^F;$^F-=eof}{"@_"=~/[12]$/&&print"$&:$`"whi +le(@_=each%h)' t1.csv t2.csv 1:8 1:6 2:1 2:3 2:5
    As always with hashes, I can't promise anything about the order of the output, but that seems to be okay. :)

    Update:
    79, by using 2-@ARGV instead of $^F
    78, by using the implicit print from -p
    77, by splitting manually instead of with -a
    #--------1---------2---------3---------4---------5---------6---------7 +------- #234567890123456789012345678901234567890123456789012345678901234567890 +1234567 perl -pe'@h{/([^,]*)/}|=2-@ARGV}while(@_=each%h){$_="@_"=~/[12]$/&&"$& +:$`$/"'

      That's neat! I had to run away to perlvar to work out what $^F was. Uniquely for a while now, I had absolutely no idea.

      That is a genuinely perverse piece of lateral thinking. ++.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.