There are a couple of problems with most of the solutions to this problem posted so far.

They look great when applied to $x and $y, but...

  1. Once you get the syntax for the hash references they don't look so golfed anymore,
  2. Unless I have translated them wrongly, they don't actually work. They all miss on the ff case because they don't test for the existance of the key in the old hash.

I couldn't get aristotle's to run, because of the error shown in the comment below his code.

I didn't test Jeurd's entry as I didn't have FreezeThaw installed. It seems like a complicated way of disabling warnings though?

The results:

C:\test>201391 Required: dd ff aa hh cc ------------------------------- BrowserUK golf: dd ff aa hh cc sauoq No warns: dd aa hh cc Sauoq golf: dd aa hh cc Elusion golf: aa cc dd BUK2 golf: dd ff aa hh cc

I attempted to translate the original code into a function where required and golf them all to an equal standard observing warnings, strict and safety. There maybe a few characters that could be trimmed from all of them without altering the original symantics.

The code:

#! perl -sw use strict; my %foo = ( aa => { O => 'zz', C => 'yy' }, bb => { O => 'xx', C => 'xx' }, cc => { O => undef, C => 'ww' }, dd => { O => 'vv', C => undef }, ee => { O => undef, C => undef }, ff => { C => undef }, gg => { O => 0, C => 0 }, hh => { O => undef, C => 0 }, ); sub BUK{ local($^W,$a)=(0,pop); grep{!exists$a->{$_}{O}or$a->{$_}{O}ne$a->{$_}{C}}keys %$a; } sub snw{ no warnings qw(uninitialized); my$h=pop; grep{$h->{$_}{O}ne$h->{$_}{C}}keys %$h; } sub sg_{ my$h=pop; # defined($x &&$y) &&$x eq$y grep{!(defined($h->{$_}{O}&&$h->{$_}{C})&&$h->{$_}{O}eq$h->{$_}{C} # ||!defined($x ||$y) ||!defined($h->{$_}{O}||$h->{$_}{C}))}%$h; } sub eg_{ my$h=pop; # (($x ||$;)eq($y ||$;)) grep{!(($h->{$_}{O}||$;)eq($h->{$_}{C}||$;))}%$h; } =pod sub ag_{ my$h=pop; grep sub{(1==grep defined,@_)or($_[0]ne$_[1])} ->(values @{$h->{$_}}['O','C']), keys %$h; } Type of arg 1 to values must be hash (not array slice) at C:\test\201391.pl line 26, near "])" =cut print " Required: dd ff aa hh cc\n"; print "-------------------------------\n"; print "BrowserUK golf: @{[BUK(\%foo)]}\n"; print "sauoq No warns: @{[snw(\%foo)]}\n"; print " Sauoq golf: @{[sg_(\%foo)]}\n"; print " Elusion golf: @{[eg_(\%foo)]}\n"; #print "Aristotle golf: @{[ag_(\%foo)]}\n"; =pod It seemed to me that the biggest problem with the original problem was + the method of recording the old versus changed hash--by adding an ex +tra key. Better I think to uses two hashes, %old and %change (or %o %o here). T +hat makes the problem of noting the changes somewhat clearer I think. + Though it does little better in the golf stakes. =cut my %o=(aa=>'zz', bb=>'xx', cc=>undef, dd=>'vv', ee=>undef, gg=>0, hh=> +undef,); my %c=(aa=>'yy', bb=>'xx', cc=>'ww', dd=>undef, ee=>undef, ff=>undef, +gg=>0, hh=>0,); sub comp{ local($^W, $a, $b)=(0, @_); grep{!exists$a->{$_} or $a->{$_} ne $b->{$_} } keys%$b; } print " BUK2 golf: @{[comp(\%o,\%c)]}\n";

Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!

In reply to Re: (Golf) Warningless Comparison by BrowserUk
in thread (Golf) Warningless Comparison by tadman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.