On May 4, 2005, at 3:45, Wijaya Edward wrote:
Thanks so much, Xavi.
Sure!
Yes it works just as I wanted.Is that what you wanted?
Excellent.
This is my final subroutine using your suggestion. I modified a bit using "reverse" because the order of the concatenated strings matter.
The last comment in the mail was about this.
sub get_item_set { use Algorithm::ChooseSubsets; my %hoa = @_;
my $iter = Algorithm::ChooseSubsets->new([keys %hoa], 2, 1);
while (my $keys = $iter->next) { my $composite_key = join '', reverse @$keys;
This approach is broken because the order in which keys(%hoa) returns the keys is random, and this order might be inherited by the iterator (or at least you can't assume this is not the case, won't change in its next release, etc.), so you are reversing an array with elements in an _unknown_ relative order! See the problem? That order in recent perls may indeed change even between different runs or perl (for security reasons explained in perlsec).
What this means is that sometimes $keys will be ['t1', 't2'], and sometimes ['t2', 't1'], from that implementation you can't tell. If you need some specific choice, to get the composite key 't1t2' instead of 't2t1', the subroutine could explicit some order in the arrayref of keys before the iterator is constructed:
my @sorted_keys = sort { ... } keys %hoa; my $iter = Algorithm::ChooseSubsets->new([EMAIL PROTECTED], 2, 1);
Then that'd be also broken because we'd depend on the implementation of Algorithm::ChooseSubsets, since from its point of view both ['t1', 't2'] and ['t2', 't1'] are valid representatives of the subset {'t1', 't2'} the module can perfectly return any of them. In particular the chosen one could change in its next release because that's OK with its interface.
So if you can insert the sort criteria down the while block that'd be the true robust place to fix the subroutine. We wouldn't sort the keys for the iterator, but for the construction of $composite_key:
my %hoa = @_; my $iter = Algorithm::ChooseSubsets->new([keys %hoa], 2, 1); while (my $keys = $iter->next) { # given a representative in $keys generate the one we need my $composite_key = join '', sort { ... } @$keys;
Whether a sort is fine or not depends on details of the problem to solve though, but you see the issues to take into account.
-- fxn
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>