[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Re: [PATCH] perlsyn: equivalent code for HASH ~~ ARRAY



In article
<b77c1dce0806080745k3e603938sad946b0f71de8b2b@mail.gmail.com>, Rafael
Garcia-Suarez <rgarciasuarez@gmail.com> wrote:

> 2008/6/8 brian d foy <brian.d.foy@gmail.com>:
> >
> > I've adjusted the example in the smart matching table in perlsyn. The
> > equivalent Perl for smart matching an array and a hash checks that all
> > of the array elements are keys in the hash, so you have to check that
> > grep returns the number of input elements, not just at least one
> > element.
> 
> Thanks, appleid as #34027.

Zsbán Ambrus wrote to me to say that this patch may be wrong, but we
get different results. As before, I'm not concerned about what it
should be as long as we document it right. However, I don't think
whatever it is doing now is what anyone intended.

And, as before, I'm willing to write test cases as long as someone can
define what correct behavior actaully is. :)

So I made another test, thinking I didn't try enough cases previously.
Can people see what they get for the same code?

   #!/usr/local/bin/perl5.10.0
   
   use 5.010;
   use Data::Dumper;
   
   my %hash = map { $_, 1 } qw( a b c );
   my @keys = keys %hash;
   my @array = @keys;
   
   print Dumper( \%hash, \@array );
   
   say " 0. ", (   {} ~~ [  ]                  ) ? "works" : "fails";
   say " 1. ", (%hash ~~ [  ]                  ) ? "works" : "fails";
   say " 2. ", (%hash ~~ [ qw( a ) ]           ) ? "works" : "fails";
   say " 3. ", (%hash ~~ [ qw( a d ) ]         ) ? "works" : "fails";
   say " 4. ", (%hash ~~ [ qw( a c ) ]         ) ? "works" : "fails";
   
   say " 5. ", (%hash ~~ @array                ) ? "works" : "fails";
   pop @array;
   say " 6. ", (%hash ~~ @array                ) ? "works" : "fails";
   
   
   say " 7. ", (%hash ~~ @keys                 ) ? "works" : "fails";
   say " 8. ", (%hash ~~ [ keys %hash ]        ) ? "works" : "fails";
   say " 9. ", (%hash ~~ [ keys %hash, qw(g) ] ) ? "works" : "fails";
   
   
   {
   my $a = {"k", "v"}; 
   my $b = ["k", "l"]; 
   say "10. ", ($a ~~ $b) ? "works" : "fails";
   }

On my MacBook, I get passes on when the list has all the keys of the
hash, and nothing more or less. Look at 5 and 6, for instance where 
I just remove a key.

According to my patch, 0, 1, 2, 4, 5, 6, 7,  8, 9 should work.

Also notice the trival cases 0 and 1.  When both the hash and array are
empty, the smart match passes. When only the array is empty, it
doesn't. Weird.


   $VAR1 = {
           'c' => 1,
           'a' => 1,
           'b' => 1
         };
   $VAR2 = [
           'c',
           'a',
           'b'
         ];
    0. works
    1. fails
    2. fails
    3. fails
    4. fails
    5. works
    6. fails
    7. works
    8. works
    9. fails
   10. fails


References to:
brian d foy <brian.d.foy@gmail.com>
"Rafael Garcia-Suarez" <rgarciasuarez@gmail.com>

[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]