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


In reply to Re: not able to get last index of array by haukex
in thread not able to get last index of array by lakshmikant

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.