In the following example:
my $info = 'NARIKANRU';
my @first_one = split /,/, $info;
print "$first_one[$#first_one]$first_one[$#first_one-1]\n";
The output is:
NARIKANRUNARIKANRU
I am not sure if this is correct because there is only one element in @first_one and $#first_one is the index of that element.
Given that $#first_one is not equal to $#first_one - 1, why is the single element in @first_one being printed twice?
The output is correct. There are no commas in
$infososplitreturns a list consisting of a single element.$#first_oneis the index of the last (and in this case also first and only) element of@first_one. With a single element,$#first_oneis0. Therefore,$first_one[$#first_one]is$first_one[0].Also,
$first_one[$#first_one-1]is$first_one[-1], i.e. the short hand way of referring to the last element of an array.Of course, things would not have worked out this way had $[ been set to some other value than the default.Apparently, things work unless$[is negative.See also perldoc perldata.
Finally, you ask if there is anything wrong in your code. From what I can see, you are not using strict and warnings. You should.