Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: writing array as pairwise

by sn1987a (Chaplain)
on Jan 23, 2015 at 16:20 UTC ( #1114305=note: print w/replies, xml ) Need Help??


in reply to writing array as pairwise

Your problem is with the nested $j loop. The starting condition should be $j=$i+1. Otherwise you will generate both [1][2] and [2][1].

Replies are listed 'Best First'.
Re^2: writing array as pairwise
by AWallBuilder (Beadle) on Jan 23, 2015 at 20:53 UTC

    That was it. Thanks. Sorry I didn't properly explain my request. basically, if I have an array A,B,C,D. I want to print out AB AC AD BC BD CD

      Oh. See also Algorithm::Loops, Algorithm::Permute (and others, I'm sure). Also, an iterator approach:

      c:\@Work\Perl>perl -wMstrict -le "sub Iterator (&) { return $_[0]; } ;; sub pairs { my @set = @_; my ($i, $j) = (0, 0); ;; return Iterator { $j = ++$i + 1 if ++$j > $#set; ($i, $j) = (0, 0) and return unless $i < $#set; return [ @set[ $i, $j ] ]; }; } ;; my $abcd = pairs(qw(a bee c d)); while (my $p = $abcd->()) { printf qq{(@$p) }; } print ''; ;; while (my $p = $abcd->()) { printf qq{(@$p) }; } " (a bee) (a c) (a d) (bee c) (bee d) (c d) (a bee) (a c) (a d) (bee c) (bee d) (c d)


      Give a man a fish:  <%-(-(-(-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1114305]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (11)
As of 2019-04-23 11:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (117 votes). Check out past polls.

    Notices?