in reply to (Golf) Warningless Comparison
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...
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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: (Golf) Warningless Comparison
by demerphq (Chancellor) on Sep 29, 2002 at 19:31 UTC | |
by BrowserUk (Patriarch) on Sep 29, 2002 at 19:47 UTC | |
by demerphq (Chancellor) on Sep 29, 2002 at 20:24 UTC | |
by BrowserUk (Patriarch) on Sep 29, 2002 at 22:35 UTC | |
by demerphq (Chancellor) on Sep 30, 2002 at 10:21 UTC | |
| |
|
Re: Re: (Golf) Warningless Comparison
by Juerd (Abbot) on Sep 29, 2002 at 19:59 UTC | |
by BrowserUk (Patriarch) on Sep 30, 2002 at 00:47 UTC |