Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
go ahead... be a heretic
 
PerlMonks  

where to look up "funny looking" perl notation (and an example)

by glwtta (Hermit)
on Jan 21, 2003 at 20:02 UTC ( #228816=perlquestion: print w/ replies, xml ) Need Help??
glwtta has asked for the wisdom of the Perl Monks concerning the following question:

Silly question for the monks:

ok, when I have something like this: $#{$some_var[$i]} and I don't know what it does, where is a good place to look these things up? Googling isn't exactly heplful when you want to find '$#{'

Also, what does the above notation, infact, mean?

Comment on where to look up "funny looking" perl notation (and an example)
Download Code
Re: where to look up "funny looking" perl notation (and an example)
by sschneid (Deacon) on Jan 21, 2003 at 20:07 UTC
    Luckily enough, your answer can be found in the documentation that comes with perl itself! Check out perldoc perldata from the command line.
Re: where to look up "funny looking" perl notation (and an example)
by Mr. Muskrat (Abbot) on Jan 21, 2003 at 20:08 UTC
    perldoc perldata would have told you that $# means 'the last index of array'. In this case, it's the last index of the array referenced by $some_var[$i].

    See also: References quick reference.

      d'oh. guess that's a clear case of lack of RTFMing

      Since we are on the subject, am I right in thinking that the 'last index' will always be one less than scalar @array? Or is there something more to it?

        You are correct...

        Once again, perldoc perldata to the rescue...

        scalar(@whatever) == $#whatever + 1;

        Almost always true...

        If it wasn't for the fact that you can change the base-index of perl arrays to something other then 0... But I've never seen anyone use that feature yet. The global parameter for the feature is '$['.

        So use $# to get the last defined element index so you can safely add (or loop), and use scalar @array to find out how many elements there are in the array...

Re: where to look up "funny looking" perl notation (and an example)
by scain (Curate) on Jan 21, 2003 at 20:15 UTC
Re: where to look up "funny looking" perl notation (and an example)
by pg (Canon) on Jan 21, 2003 at 20:50 UTC
    1. As others already pointed out that most likely your $some_var[$i] is an array reference.

      However, just for completeness, I want to point out that it could also be a symblic reference. If use strict is truned off for refs, that would be also correct from a syntax point of view.

      However I am not a supporter of the symbolic ref usage.

    2. It is worth to mention to new perl users, that $# can be used as lvalue.

      This is extreamly useful, when you want perl to garbage collect your big arrays.

      Just do
      @a = (1..1000); print "original array:\n"; print join(",", @a), "\n"; $#a = -1; print "after set to -1:\n"; print join(",", @a), "\n";#You can not say for sure that your array ha +s already been garbage collected at this point, but at least it is no +w a candidator.
      For the sake of garbage collection - if you are 'deleting' the whole array - I prefer the more readable @array = ();

      -- Hofmator

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-04-17 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (440 votes), past polls