in reply to array problems
How about this?
#! perl -slw use strict; use vars qw[ $a $b ]; use List::Util qw[ reduce ]; my $op = shift @ARGV; my @hasharray; for ( 2 .. @ARGV ) { for my $indices ( Cnr( $_, 0..$#ARGV ) ) { push @hasharray, { LIST=> [ @$indices ], VALUE=> reduce{ my $val = eval "$a $op $b"; } @ARGV[ @$indices ] }; } } print "@{ $_->{LIST} } : $_->{VALUE}" for @hasharray; exit; sub Cnr{ my( $n, @r ) = shift; return [] unless $n--; for my $x ( 0 .. ($#_ - $n) ) { push @r, map{ [ $_[$x], @$_ ] } Cnr( $n, @_[ ($x+1) .. $#_ ] ) + ; } return @r; }
Supply the (binary) operator as the first argument, the rest will be used as the values in the array.
A few runs
P:\test>283241 + 1 2 3 4 0 1 : 3 0 2 : 4 0 3 : 5 1 2 : 5 1 3 : 6 2 3 : 7 0 1 2 : 6 0 1 3 : 7 0 2 3 : 8 1 2 3 : 9 0 1 2 3 : 10 P:\test>283241 - 1 2 3 4 0 1 : -1 0 2 : -2 0 3 : -3 1 2 : -1 1 3 : -2 2 3 : -1 0 1 2 : -4 0 1 3 : -5 0 2 3 : -6 1 2 3 : -5 0 1 2 3 : -8 P:\test>283241 * 1 2 3 4 0 1 : 2 0 2 : 3 0 3 : 4 1 2 : 6 1 3 : 8 2 3 : 12 0 1 2 : 6 0 1 3 : 8 0 2 3 : 12 1 2 3 : 24 0 1 2 3 : 24 P:\test>283241 / 1 2 3 4 0 1 : 0.5 0 2 : 0.333333333333333 0 3 : 0.25 1 2 : 0.666666666666667 1 3 : 0.5 2 3 : 0.75 0 1 2 : 0.166666666666667 0 1 3 : 0.125 0 2 3 : 0.0833333333333332 1 2 3 : 0.166666666666667 0 1 2 3 : 0.0416666666666667
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: array problems
by bfish (Novice) on Aug 12, 2003 at 20:58 UTC | |
by BrowserUk (Patriarch) on Aug 12, 2003 at 21:35 UTC | |
by bfish (Novice) on Aug 13, 2003 at 14:49 UTC | |
by BrowserUk (Patriarch) on Aug 13, 2003 at 15:13 UTC | |
|
Re: Re: array problems
by bean (Monk) on Aug 12, 2003 at 20:22 UTC | |
by BrowserUk (Patriarch) on Aug 12, 2003 at 20:32 UTC | |
|
Re: Re: array problems
by bfish (Novice) on Aug 12, 2003 at 20:45 UTC |