Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks: I am stumped as to what kind of loop (my guess) structure I would need to accomplish this paired numbers sequence; a pyramid number sequence (best description)... and no this is not homework... this is the (x,y) pair set that I am trying to come up with:
1,2 2,3 3,4 4,5 5,6 6,7 1,3 2,4 3,5 4,6 5,7 1,4 2,5 3,6 4,7 1,5 2,6 3,7 1,6 2,7 1,7
I am betting that it is some kind of simple nested loop structure, but, again, I am really not sure where to begin. Sometimes the blantantly obvious can be quite elusive.
Thank you in advance for your wisdom.

Replies are listed 'Best First'.
Re: generating pyramid number sequence
by japhy (Canon) on Jan 19, 2006 at 18:08 UTC
    If you re-order the pairs, you see (1,2), (1,3), (1,4), (1,5), (1,6), (1,7), and then (2,3), (2,4), (2,5), (2,6), (2,7), and then (3,4), etc.

    To that end, you're doing two loops: one from 1 to 7 (call this X) and the other from X+1 to 7.

    for $x (1 .. 7) { for $y ($x+1 .. 7) { push @pairs, [$x,$y]; } }
    If you truly need the pairs in the order you've shown, be trickier.

    Update: by "be trickier", I mean, there is also a discernable pattern to the data set in the sequence you've shown. It's not X = (1 .. 7), Y = (X+1 .. 7), it's something else. It's up to you to recognize the pattern. I'll do the hard part for you:

    for $x (? .. ?) { for $y (? .. ?) { push @pairs, [$y, $y-$x]; } }

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: generating pyramid number sequence
by artist (Parson) on Jan 19, 2006 at 18:53 UTC
    $N = 6; for $i (reverse(1..$N)){ for $j (1..$i){ print "$j,",$j+1-$i+$N,","; } print "\b\n"; }
    (Update:Generalized Version)
    --Artist
Re: generating pyramid number sequence
by duckyd (Hermit) on Jan 20, 2006 at 02:14 UTC
    as much as it pains me to use a c style for....
    my $n=7; for my $a ( 1.. $n-1){ for( my $b = 1; $b+$a <= $n; $b++){ print $b, ',', $b+$a, ' '; } print "\n"; }
      ah, this is more perlish:
      my $n=7; for my $a ( 1..$n-1){ print $_, ',', $_+$a, ' ' for (1..$n-$a); print "\n"; }
        Maybe this one is more "perlish" but the first one you posted is certainly easier to read!
Re: generating pyramid number sequence
by pKai (Priest) on Jan 22, 2006 at 21:44 UTC

    If you are willing to take a little skewed view on the "triangle", it can also be written as:

    1,2 1,3 2,3 1,4 2,4 3,4 1,5 2,5 3,5 4,5 1,6 2,6 3,6 4,6 5,6 1,7 2,7 3,7 4,7 5,7 6,7

    Now former diagonals form lines/rows and in this presentation "x" is constant in columns and "y" is constant in rows.

    The above output can be generated e. g. by the following obfu code oneliner:
    Win command line here; change delimiter from double- to single-quotes for other shell, if applicable

    perl -Mstrict -wle "$.++;print qq(@{[map qq{$_,$.}, 1..++$.-1]}) while $.<7"

Re: generating pyramid number sequence
by turo (Friar) on Jan 22, 2006 at 01:30 UTC

    I'm suppose i'm late :'( ...

    Japhy solution solves the problem in a very smart way, i like it too much.

    But I have written another (before read any solution ^_^), i put it here for the collection :-)

    $max = 7; for ($i=1; $i<$max; $i++) { @tuple = (0,$i); print ++$tuple[0],',',++$tuple[1],' ' while ($tuple[1] < $max) +; print "\n" }


    Update

    ... and Here a dirty version ...
    for ($M=7, $i=1; @_ = (0,$i), $i<$M; $i++) { print ++$_[0],',',++$_[1],($_[1]==$M?"\n":" ") while ($_[1] +< $M) }

    turo

    perl -Te 'print map { chr((ord)-((10,20,2,7)[$i++])) } split //,"turo"'