Here is a program that does the swapping with one temp only:

use strict; use warnings; my @buffer = qw[X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 Y0 Y1 Y2 Y3 Y4 Y5 Y6 Y7] +; sub swapsamesize { my( $s1, $s2, $n, $buf ) = @_; ($$buf[$s1+$_], $$buf[$s2+$_]) = ($$buf[$s2+$_], $$buf[$s1+$_]) for +0..$n-1; # too lazy to use $tmp } sub swapbuffers { my( $n1, $buf ) = @_; my $ntotal = @$buf; my $n2 = $ntotal - $n1; my $d = $n1 - $n2; print "$n1 $n2 $ntotal $d\n"; print "@$buf\n"; my( $s1, $s2, $n ) = ( 0, $n1, $n2 ); swapsamesize( $s1, $s2, $n, $buf ); print "@$buf\n"; ( $s1, $s2, $n ) = ( $n2, $ntotal - $d, $d ); swapsamesize( $s1, $s2, $n, $buf ); print "@$buf\n"; ( $s1, $s2, $n ) = ( $n2, $ntotal - 2 * $d, $d ); swapsamesize( $s1, $s2, $n, $buf ); print "@$buf\n"; ( $s1, $s2, $n ) = ( $n2, $ntotal - 3 * $d, $d ); swapsamesize( $s1, $s2, $n, $buf ); print "@$buf\n"; ( $s1, $s2, $n ) = ( $n2, $ntotal - 4 * $d, $d ); swapsamesize( $s1, $s2, $n, $buf ); print "@$buf\n"; } swapbuffers 10, \@buffer;

Of course, the repeated calls to the utility function swapsamesize should be done in a loop until all is done. It also only works if the overlap when swapping (i.e. n1 - n2) is smaller than the rest of the smaller half. I think the logic can be adapted...


In reply to Re: [OT] Swapping buffers in place. by hdb
in thread [OT] Swapping buffers in place. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.