EDIT:

After reading the responses to my post, I've come to two tentative conclusions: 1) I have only a loose grasp of how references work in Perl. It's obvious I have a lot to learn on the subject. 2) In this case, I'm mistaking output for content (Data::Dumper does not represent the reference structure's content in the way I imagined). 3) This is frustrating but fun as hell.


Here's my edited code that gives me what I want (thank you all for helping me with this):
sub inc{ my $ref=shift; my $span=shift; $span?($span--):($span=1); my @b=map{ clone([@$ref[$_-$span..$_]]) }($span..$#$ref); \@b } sub clone{#clone references my $ref=shift; my@array=map{ ref($_)?clone($_):$_ }@$ref; \@array } my@b=(1..5); my $c = inc(inc(\@b)); print Dumper $c;
Which produces:
$VAR1 = [ [ [ 1, 2 ], [ 2, 3 ] ], [ [ 2, 3 ], [ 3, 4 ] ], [ [ 3, 4 ], [ 4, 5 ] ] ];
Woohoo!

ORIGINAL POST:

I have come across a strange thing. If I run the following code with an array of scalars, then I get what I expect. E.g.,
@a = (1,2,3,4,5,6); increment(\@a); #produces tuples of: [1,2],[2,3],[3,4],[4,5],[5,6] #yay!!
But, I want to run increment() with arrays of arrays. But, the result is all sorts of funky. Here's Data::Dumper output:
$VAR1 = [ [ [ 1, 2 ], [ 2, 3 ] ], [ $VAR1->[0][1], [ 3, 4 ] ], [ $VAR1->[1][1], [ 4, 6 ] ] ];
I'm using the following test code:
my @a = (1,2,3,4,6); print Dumper increment(increment(\@a)); sub increment{ my $d=shift; my @b; for my $i (1..$#$d){ my @a = @$d[$i-1..$i]; push @b,\@a; } \@b }
The output is half-right --the second part of each tuple is itself a tuple (and is what's expected). But, what's with the first part of each tupple? The "$VAR->[n][1]"

I am flummoxed.


Thanks much for whatever advice you might have.



$state{tired}?sleep(40):eat($food);

In reply to Array of Arrays: why is "$VAR1->[0][1]" and the like embedded within? by corenth

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.