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

I am trying to get last index of array but its printing the full array , please let me know where I am doing mistake

@numbers = (15,5,7,3,9,1,20,13,9,8, 15,16,2,6,12,90); @a = join ("," ,(sort {$a <=> $b} @numbers)); print "@a\n"; print $a[-2];

Replies are listed 'Best First'.
Re: not able to get last index of array
by haukex (Archbishop) on Jun 01, 2016 at 14:21 UTC

    Hi lakshmikant,

    Tip #4 from the Basic debugging checklist: Print your values using Data::Dumper to see what they really contain.

    use Data::Dumper; my @numbers = (15,5,7,3,9,1,20,13,9,8,15,16,2,6,12,90); my @a = join ("," ,(sort {$a <=> $b} @numbers)); print Dumper(\@a); __END__ $VAR1 = [ '1,2,3,5,6,7,8,9,9,12,13,15,15,16,20,90' ];

    It looks a little misleading, but the quotes tell you: @a is an array with a single element, a string which contains your numbers joined by commas.

    Tip: Don't join your array except for printing, e.g.

    my @numbers = (15,5,7,3,9,1,20,13,9,8,15,16,2,6,12,90); my @a = sort {$a <=> $b} @numbers; print join(",",@a), "\n"; print $a[-2], "\n"; __END__ 1,2,3,5,6,7,8,9,9,12,13,15,15,16,20,90 20

    Also, note that $a[-2] gets the second-to-last element; you can get the last one via $a[-1], or even $a[$#a], since $#a gives you the last index of the array @a. (Update: ww demonstrated it here)

    Hope this helps,
    -- Hauke D

Re: not able to get last index of array
by ww (Archbishop) on Jun 01, 2016 at 14:16 UTC

    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.

      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:  <%-{-{-{-<

Re: not able to get last index of array
by hippo (Archbishop) on Jun 01, 2016 at 14:15 UTC

    Your use of join means that the array @a has only one element which is a big long string. Omit the join and you should get a bit farther.

Re: not able to get last index of array
by Marshall (Canon) on Jun 01, 2016 at 18:41 UTC
    I'm not quite sure what you want here. The numeric sort and your question implies that you want the max value? If that is what you want:
    #!/usr/bin/perl use strict; use warnings; use List::Util qw(max); #CORE nothing to install # changed the order to make max value # in the middle of @numbers my @numbers = (15,5,7,3,90,9,1,20,13,9,8, 15,16,2,6,12); print "max is =", max(@numbers), "\n"; __END__ max is =90
    Update: I thought that I should point out that you can re-assign the sorted array back to the same variable....
    #!/usr/bin/perl use strict; use warnings; # changed the order to make max value # in the middle of @numbers my @numbers = (15,5,7,3,90,9,1,20,13,9,8, 15,16,2,6,12); @numbers = sort {$a <=> $b} @numbers; print "max = $numbers[-1]\n"; print "next to max = $numbers[-2]\n"; __END__ max = 90 next to max = 20
    And yes, if 90 appeared twice, this changes things as both of these would be the same.
Re: not able to get last index of array
by Laurent_R (Canon) on Jun 01, 2016 at 20:37 UTC
    $ perl -e 'print ((sort {$a <=> $b} 15,5,7,3,9,1,20,13,9,8,15,16,2,90, +6,12)[-1])' 90
    Please also note that there are more efficient ways than sorting to find the max of a list or an array.
Re: not able to get last index of array
by anonymized user 468275 (Curate) on Jun 02, 2016 at 11:43 UTC
    If you only want the maximum value, you don't need to sort, e.g.:
    my @numbers = (15,5,7,3,9,1,20,13,9,8, 15,16,2,6,12,90); printf ('The maximum number is %s%s', max(@numbers), "\n"); sub max { my $first = shift; while ($#_ >= 0) { # until list exhausted if ($first >= $_[0]) { # keep throwing away inferiors shift; } else { $first = shift; # crown the new king } } return $first; # end of list, inferiors exhausted }

    One world, one people

      Quite. It's even simpler if you know that List::Util has been in core for the last decade and has its own max() function.