Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: not able to get last index of array

by ww (Archbishop)
on Jun 01, 2016 at 14:16 UTC ( [id://1164679]=note: print w/replies, xml ) Need Help??


in reply to not able to get last index of array

This is a FAQ: Answer: How do I find the index of the last element in an array? (There are multiple answers here)

This may not be the answer you're seeking: if not, please clarify your question.

Update: And this may clarify the answers (here and above):

#!/usr/bin/perl use 5.018; use strict; use warnings; # 1164677 my @numbers = (15,5,7,3,9,1,20,13,9,8,15,16,2,6,12,90); (my @newstring) = sort {$a <=> $b} @numbers; for (@newstring) { print "$_ "; } say "\nNumber of elements: " . @newstring; # number of e +lements my $lastindex = $#newstring; say "\$lastindex: $lastindex"; say "minus 2: " . $newstring[-2]; # "@a" provokes an "unintended in +terpolation" warning say "minus 1: " . $newstring[-1]; =head output 1 2 3 5 6 7 8 9 9 12 13 15 15 16 20 90 Number of elements: 16 $lastindex: 15 minus 2: 20 minus 1: 90 =cut

Spirit of the Monastery

If I've misconstrued your question or the logic needed to answer it, I offer my apologies to all those electrons which were inconvenienced by the creation of this post.

Replies are listed 'Best First'.
Re^2: not able to get last index of array
by Lotus1 (Vicar) on Jun 04, 2016 at 15:36 UTC

    Hi ww,

    When I run the OP's script with use warnings; added I don't see a warning for printing "@a" but I do get one for print $a[-2] since there is only one element in the OP's array. Here is the warning I get:

    Use of uninitialized value in print at C:\usr\pm\sort\1164677.pl line 7.

    When you print an array inside of double quotes it adds a space by default between the elements or whatever the output field list separator, "$," '$"', is set to. How do I print an array with commas separating each element?

    Update: I forgot to mention also at the line (my @newstring) = sort {$a <=> $b} @numbers; you don't need the parentheses since defining an array is already in array context. These are nitpicky comments since your program works and answered the OPs questions, but it's my 2 cents worth.

    Update: Fixed the separator as pointed out by AnomalousMonk

      When you [interpolate] an array inside of double quotes it adds ... the output field separator, "$,", ...

      Array interpolation adds the list separator  $" between elements. See perlvar.


      Give a man a fish:  <%-{-{-{-<

        Thanks AnomalousMonk

        If I had read more closely the node I linked to it explains the difference between $, and $" . I just knew there was a variable involved and grabbed the first one I saw.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1164679]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-19 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found