A comment in the CB made me wonder if I could write $#{$#a} for an array @a and have it actually do something. For this, I would need $#a to actually be an array reference. Much to my surprise, this could actually be accomplished! (perl, v5.8.3 built for i386-linux-thread-multi)

Of course, setting $#a=$b has the (side-)effect of expanding @a to a huge size, so these examples take a few seconds (or may run out of memory even):

% perl -le '$b=[1..4]; $#a=$b; print $#{$#a}' 3
Can this actually be doing what I think it is?? Indeed, it looks as though $#a is actually the same reference as $b$:
% perl -le '$b=[1..4]; $#a=$b; print @{$#a}; push @$b, 5; print @{$#a} +' 1234 12345
In fact, this also works when $b is lexical, so it's not a symbolic reference.

You can put other types of references into $#a, and dereference them in any standard way:

% perl -le '$b=[1..4]; $#a=$b; print $#a->[2]' 3 % perl -le '$b=\"foo"; $#a=$b; print ${$#a}' foo % perl -le '$b={foo=>1}; $#a=$b; print keys %{$#a}' foo
However, you cannot preserve blessed-ness of a scalar:
% perl -le 'sub foo{"foo"} $b=bless[]; $#a=$b; print $#a->foo' Can't call method "foo" without a package or object reference at -e li +ne 1.
This is our first clue that this is not a fully-fledged reference.. we also see that ref is oblivioius to it:
% perl -le '$b=[1..4]; $#a=$b; print ref $#a' [nothing]
Also, I can't copy $#a into a variable and then use that variable as a reference:
% perl -le '$b=[1..4]; $#a=$b; $x=$#a; print @$x' [nothing]
In fact, after I copy $#a into a variable, I can't even use $#a itself as a reference anymore! Hello Heisenberg!
% perl -le '$b=[1..4]; $#a=$b; $x=$#a; print @{$#a}' [nothing]
Very odd.

So, Does anyone know what's going on? I would have expected this to behave either kinda like hash keys (which are always string values, and are not retrieved as full scalars with the requisite magic) or just a normal scalar. What seems to be happening to $#a is a little bit of both.

Remember, this is purely for curiosity's sake, as using $#a as a reference is quite mad.

PS: It is a damn shame that the following code runs out of memory, it would make quite a bizarre JAPH:

#/usr/bin/perl -l my@a=qw[just another perl hacker];$#a=\@a; print "@{$#{$#{$#{$#{$#{$#{$#{$#a}}}}}}}}"
(the out of memory error I get is another mystery -- if I replace the @ with an $#, I don't crash)

blokhead


In reply to Putting weird things into $#array by blokhead

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.