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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |