duelafn has asked for the wisdom of the Perl Monks concerning the following question:
This produces the output:use strict; use warnings; use Group::Permutation; my $x = new Group::Permutation qw(1 3 2 4); print "x = $x\n"; print 'x of 2 = ',$x->of(2),"\n"; print 'ref of x of 2: ', ref $x->of(2),"\n"; print "x of 2 is three!\n" if ($x->of(2) == 3);
Most notably, the test ($x->of(2) == 3) fails. I can't figure out why this happens.x = (1, 3, 2, 4) x of 2 = 3 ref of x of 2:
andsub of { my $x = shift; my @y = map { $_-1 if defined && /^\d+$/ && $_>0 } @_; # protect the caller from placement errors return undef unless ($#y == $#_); return map { ($_<$$x{'n'}) ? $$x{'perm'}[$_] + 1 : $_+1 } @y; }
The snipped sections are just some definitions and option parsing.sub new { # SNIP # BEGIN vicious error checking # all input must be legitimate $x{'perm'}=[ map { $_-1 if defined and /^\d+$/ and $_ > 0} @_ ]; return undef unless $#{$x{'perm'}} == $#_; # it must really be a permutation of 0..n-1 return undef unless join(' ',sort { $a <=> $b } @{$x{'perm'}}) eq +join(' ',0..$#{$x{'perm'}}); # END error checking # SNIP $x{'n'} = scalar @{$x{'perm'}}; # for clarity in other places bless \%x, $class; }
Thanks,
-Dean
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (x == 3 && x != 3)
by no_slogan (Deacon) on Mar 11, 2002 at 22:22 UTC | |
by merlyn (Sage) on Mar 11, 2002 at 23:42 UTC | |
by duelafn (Parson) on Mar 11, 2002 at 22:33 UTC | |
|
Re: (x == 3 && x != 3)
by erikharrison (Deacon) on Mar 11, 2002 at 20:36 UTC | |
|
Re: (x == 3 && x != 3)
by duelafn (Parson) on Mar 11, 2002 at 21:52 UTC |