Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Where is $# fully documented?

by LanX (Saint)
on Sep 23, 2014 at 14:08 UTC ( [id://1101642]=perlquestion: print w/replies, xml ) Need Help??

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

Hi

I'm struggling to find the perldocs where the usage of $# (i.e. last-index) with array-references (not symbols) is documented. Can you help me?

If not, what's the best place to augment the perldocs?

Tested behavior (5.10):

DB<107> @a = (1..5) # @array => (1, 2, 3, 4, 5) DB<108> $#a # classic usage => 4 DB<109> $a = [1..10] # arrref => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] DB<110> $#$a => 9 DB<111> $a = [ map [0..$_],0..3 ] # AoA => [[0], [0, 1], [0, 1, 2], [0, 1, 2, 3]] DB<112> $#{ $a->[2] } => 2

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Replies are listed 'Best First'.
Re: Where is $# fully documented?
by ikegami (Patriarch) on Sep 23, 2014 at 14:34 UTC

    "Using References" in perlref says:

    Using References

    That's it for creating references. By now you're probably dying to know how to use references to get back to your long-lost data. There are several basic methods.

    1. Anywhere you'd put an identifier (or chain of identifiers) as part of a variable or subroutine name, you can replace the identifier with a simple scalar variable containing a reference of the correct type:

    [snip]

    2. Anywhere you'd put an identifier (or chain of identifiers) as part of a variable or subroutine name, you can replace the identifier with a BLOCK returning a reference of the correct type. In other words, the previous examples could be written like this:

    [snip]

    The first documents $#$aref, the second $#{$aref}. $aref->$#* is documented by example further down in the "Postfix Dereference Syntax" section.

Re: Where is $# fully documented? (rqr)
by tye (Sage) on Sep 23, 2014 at 15:07 UTC

    I like the distillation of the rules I came up with long ago at References Quick Reference. If that distillation or something substantially similar hasn't made it into the Perl docs, I think it would be a useful addition. I certainly still often see the same questions repeated that were a big motivation for me publishing that.

    - tye        

      Yes, Ikegami's table isn't bad either.

      But I think as a minimum $# should be listed as one of the examples in perlref#Using-References

      IMHO the only syntactic option missing.

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: Where is $# fully documented?
by ysth (Canon) on Sep 23, 2014 at 14:24 UTC

    The documentation for using references is at http://perldoc.perl.org/perlref.html#Using-References. It does not mention $# at all, but points 1 and 2 there address exactly what you are asking about. In general, the documentation does not strive to cover every possible combination of interacting features.

    --
    A math joke: r = | |csc(θ)|+|sec(θ)| |-| |csc(θ)|-|sec(θ)| |
      > It does not mention $# at all

      well in these 5.20 docs this is mentioned  $aref->$#*; # same as $#{ $aref }

      > but points 1 and 2 there address exactly what you are asking about.

      Anywhere you'd put an identifier (or chain of identifiers) as part of a variable or subroutine name

      So I suppose $# counts as a special function since its not a variable, but addressing an attribute of a variable?

      Thanks anyway! :)

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

        Special function? No.

        The a in @a is the identifier part of the variable name; the documentation addresses anywhere you'd put that a, which includes $#a.

        Update: hmm, it does say "as part of a variable...name", implying it would only work if $#a is a considered a variable. I shy away from that because, even though it is an assignable lvalue, it really is just an attribute of an array variable. So the doc could use a minor tweak.

        In any case, $#$arrayref / $#{$arrayref} ought to be added to the examples there.

        --
        A math joke: r = | |csc(θ)|+|sec(θ)| |-| |csc(θ)|-|sec(θ)| |
        $#a is no more a function and as much a variable as $a and @a.
Re: Where is $# fully documented?
by RedElk (Hermit) on Sep 23, 2014 at 14:40 UTC

    FWIW this might help.

Re: Where is $# fully documented?
by toolic (Bishop) on Sep 23, 2014 at 14:12 UTC
    I see several references to $# in perldata. UPDATE: I didn't notice you were focusing on references.
      I scanned perldata and it only mentions the classic use with an @array symbol (i.e. $#array ), but no array ref (i.e. $#$aref or $#{$aref} ).

      IIRC does perldsc show code using the trick, but w/o worded explanation.

      Is it meant to be an undocumented feature, which can possibly change in the future?

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1101642]
Approved by MidLifeXis
Front-paged by MidLifeXis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found