2009/12/15 Shawn H Corey <shawnhco...@gmail.com>:
> Philip Potter wrote:
>> 1. Is there a "set" type which holds aggregate data and doesn't care
>> about order, which I could use to compare these results for equality?
>
> You can use a hash as a set or a bag.

Yeah I thought about this -- while I can see how it works with simple
scalars, I can't see how to make it work with references:

$ cat foo.pl
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
$Data::Dumper::Maxdepth = 0;

my @a = ([1],[2],[3]);
my @b = ([1],[2],[3]);

my %set_a = map { $_ => 1 } @a;
print '%set_a : ', Dumper \%set_a;

my %set_b = map { $_ => 1 } @b;
print '%set_b : ', Dumper \%set_b;

use Test::More tests => 1;

is_deeply(\%set_a, \%set_b, 'Test that references as hash keys works');

__END__

$ perl foo.pl
1..1
%set_a : $VAR1 = {
  'ARRAY(0x1b66c40)' => 1,
  'ARRAY(0x1b66df0)' => 1,
  'ARRAY(0x1d50550)' => 1
};
%set_b : $VAR1 = {
  'ARRAY(0x1ba7010)' => 1,
  'ARRAY(0x1d1b3a8)' => 1,
  'ARRAY(0x1d84698)' => 1
};
not ok 1 - Test that references as hash keys works
#   Failed test 'Test that references as hash keys works'
#   at foo.pl line 26.
#     Structures begin differing at:
#          $got->{ARRAY(0x1d1b3a8)} = Does not exist
#     $expected->{ARRAY(0x1d1b3a8)} = '1'
# Looks like you failed 1 test of 1.

Perhaps a way of collapsing references to unique strings would work? ie:

my %set_a = map { stringify($_) => 1 } @a;

where stringify() is a function which maps references to unique
strings identifying their referent?

Phil

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to