in reply to Re: (Golf) Warningless Comparison
in thread (Golf) Warningless Comparison

I went and updated your code to incorporate my solutions, and in the process added a bunch of cases. Unfortunately this also caused your solution to generate warnings. I find it a touch suprising that my longer solution is the only one that works correctly on all the cases. Guys youve got to be more thorough in your testing!
use warnings; use strict; use Carp; local $SIG{__WARN__}=sub {die("\t@{[(caller(1))[3]]}() generated warni +ngs!\n")}; 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 }, # ii => { O => undef }, # jj => { O => 0, C => ' ' }, # kk => { O => ' ', C => 0 }, # ll => { O => undef, C => ' ' }, # mm => { O => ' ', C => undef }, # }; 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;grep{!(defined($h->{$_}{O}&&$h->{$_}{C})&&$h->{$_}{O} +eq$h->{$_}{C}||!defined($h->{$_}{O}||$h->{$_}{C}))}%$h} sub eg_{my$h=pop;grep{!(($h->{$_}{O}||$;)eq($h->{$_}{C}||$;))}%$h} sub ddf{$a=pop;grep{$b=$$a{$_};defined$$b{O}!=defined$$b{C}||defined$$ +b{O}&&$$b{O}ne$$b{C}}keys%$a}#98 sub dex{$a=pop;grep{$b=$$a{$_};exists$$b{O}!=exists$$b{C}||defined$$b{ +O}!=defined$$b{C}||defined$$b{O}&&$$b{O}ne$$b{C}}keys%$a}#126 sub tad{my($h)=@_;grep{!exists($h->{$_}->{O})||(defined($h->{$_}->{O}) +?(defined($h->{$_}->{C})?($h->{$_}->{O}ne$h->{$_}->{C}):1):defined($h +->{$_}->{C}))}keys%$h}#162 print " Required: aa cc dd ff hh ii jj kk ll mm\n"; print "---------------------------------------------\n"; eval { print " tadman golf: @{[sort {$a cmp $b} tad($foo)]}\n"; }; print $@ if $@; eval { print "BrowserUK golf: @{[sort {$a cmp $b} BUK($foo)]}\n"; }; print $@ if $@; eval { print "sauoq No warns: @{[sort {$a cmp $b} snw($foo)]}\n"; }; print $@ if $@; eval { print " Sauoq golf: @{[sort {$a cmp $b} sg_($foo)]}\n"; }; print $@ if $@; eval { print " Elusion golf: @{[sort {$a cmp $b} eg_($foo)]}\n"; }; print $@ if $@; eval { print " Demerphq defd: @{[sort {$a cmp $b} ddf($foo)]}\n"; }; print $@ if $@; eval { print "Demerphq exist: @{[sort {$a cmp $b} dex($foo)]}\n"; }; print $@ if $@; __END__ Required: aa cc dd ff hh ii jj kk ll mm --------------------------------------------- tadman golf: aa cc dd ff hh jj kk ll mm main::BUK() generated warnings! sauoq No warns: aa cc dd hh jj kk ll mm Sauoq golf: aa cc dd hh jj kk ll mm Elusion golf: aa cc dd jj kk ll mm Demerphq defd: aa cc dd hh jj kk ll mm Demerphq exist: aa cc dd ff hh ii jj kk ll mm
Hmm, maybe it isnt so suprising considering its the longest with the exception of only tadmans solution

;-)

---
demerphq
my friends call me, usually because I'm late....

Replies are listed 'Best First'.
Re: Re: Re: (Golf) Warningless Comparison
by BrowserUk (Patriarch) on Sep 29, 2002 at 19:47 UTC

    If you change the specs to the challenge, any thing is possible.

    My version does not produce warnings under the terms of the challenge. Note:the localisation and setting of $^W=0.


    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      If you change the specs to the challenge, any thing is possible.

      Advice you should take to heart. From the OP:

        Since warnings are supposed to be avoided, use strict; use warnings;

      My version does not produce warnings under the terms the unmodifies rules of the challenge. Note:the localisation and setting of $^W=0.

      The original terms of the challenge call for it to not produce warnings when run under use warnings. Yours does. The local $^W stuff doesnt have the effect you think it does when used under use warnings; It works the way you think it does when run under only "-w" but that isnt the same thing. It also doesnt work the way you think it does when run under both "-w" and use warnings;. From perllexwarn:

        If a piece of code is under the control of the warnings pragma, both the $^W variable and the -w flag will be ignored for the scope of the lexical warning.

      Also, next time try running the code before you get so insistant it works the way you think it does....

      --- demerphq
      my friends call me, usually because I'm late....

        I am aware of the differences between -w and use warnings. I chose to use -w and $^W=0 within the localised scope of a subroutine who's sole purpose was to avoid them, which is I think, a legimite tactic given this was golf and only keystrokes count.

        The reasons why my (fully tested*) code both produced warnings and apparently failed to produce the desired output, was because you ran it in a context different from that in which it was designed to run and made no attempt to correct for that change of context in any meaningful way.

        In fact you deliberately suppressed the (correct) output for reasons I can only speculate on.

        vis.

        #! perl -w use warnings; 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 }, # }; #234567890123456789012345678901234567890123456789012345678901234567890 +1234567890123 sub f{no warnings;$a=pop;grep{!exists$a->{$_}{O}or$a->{$_}{O}ne$a->{$_ +}{C}}keys%$a} # 83! print "@{[f($foo)]}\n"; __DATA__ C:\test>201598 dd ff aa hh cc C:\test>

        Even in this form, it still complies with the original spec and comes in at 83

        *My original post contained both fully working program and its output unedited. No warnings, and the required output.


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