Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Is using $#array Frowned on?

by doubleqq (Acolyte)
on Feb 13, 2014 at 20:44 UTC ( #1074891=perlquestion: print w/replies, xml ) Need Help??
doubleqq has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I recently used the '$#array' operator in a script with great success. However reading in the 'Programming Perl' book it says:

"$#, do not use this; user printf instead".

I think I might be getting my operators confused here, since I am not using the output format for printed numbers (I think). But as for finding the last index in an array, the '$#array' was invaluable. Is this practice generally considered poor form? If not recommended to use, what would be a better way?

Thanks!

Replies are listed 'Best First'.
Re: Is using $#array Frowned on?
by shmem (Chancellor) on Feb 13, 2014 at 20:54 UTC

    The special variable $# (see perlvar), which got deprecated and is not longer supported, has nothing to do with the $#array expression which denotes the last index of @array.

    It might well be that $# got deprecated because of such confusions...

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      Good $#/$#array distinction.

Re: Is using $#array Frowned on?
by Kenosis (Priest) on Feb 13, 2014 at 20:48 UTC

    $#array is perfectly acceptable for getting the value of @array's last index (i.e., the subscript of the last element).

Re: Is using $#array Frowned on?
by choroba (Bishop) on Feb 13, 2014 at 21:32 UTC
    The only place where I would frown on a $#array would be in the + 1 context where @array might be more appropriate. It depends on the semantics - for 0 .. $#array is, at least for me, the best way how to iterate over indices, when iterating over the members themselves is not possible.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Is using $#array Frowned on?
by toolic (Bishop) on Feb 13, 2014 at 20:57 UTC
    perldiag
    perl -Mdiagnostics -e '$# = 1' $# is no longer supported at -e line 1 (#1) (D deprecated, syntax) The special variable $#, deprecated in olde +r perls, has been removed as of 5.9.3 and is no longer supported. Yo +u should use the printf/sprintf functions instead.
Re: Is using $#array Frowned on?
by eyepopslikeamosquito (Chancellor) on Feb 14, 2014 at 21:06 UTC

    An example of where you should avoid the $#array notation is when counting from the end of an array (see Perl Best Practices, chapter 5, "Use negative indices when counting from the end of an array"). For example, you should replace:

    $frames[$#frames ] = $active{top}; $frames[$#frames-1] = $active{prev}; $frames[$#frames-2] = $active{backup};
    with:
    $frames[-1] = $active{top}; $frames[-2] = $active{prev}; $frames[-3] = $active{backup};
    because it is easier on the eye and avoids the undesirable repetition of the frames variable name (DRY).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1074891]
Approved by shmem
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2017-11-25 01:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (355 votes). Check out past polls.

    Notices?