Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array with element -1, can't determine source

Status
Not open for further replies.

PinkeyNBrain

IS-IT--Management
Dec 12, 2006
279
US
Found the following:
Code:
print "Index of first element $[\n";
print "pre   0 exists\n" if (exists $arr[0]);
print "pre  -1 exists\n" if (exists $arr[-1]);
push(@arr, 'foo');
print "post  0 exists\n" if (exists $arr[0]);
print "post -1 exists\n" if (exists $arr[-1]);
print "arr elements $#arr\n";
This code generates:
Code:
Index of first element 0
post  0 exists
post -1 exists
arr elements 0
Where is the -1 element coming from?
 
Hi

PinkeyNBrain said:
Where is the -1 element coming from?
Similarly with many other languages, negative indexes are calculated from right.
Code:
[gray]# positive index : $arr[0]    $arr[1]     $arr[2][/gray]
[gray]# negative index : $arr[-3]   $arr[-2]    $arr[-1][/gray]
[navy]$arr[/navy]           [teal]=[/teal] [teal]([/teal] [green][i]'first'[/i][/green][teal],[/teal]   [green][i]'second'[/i][/green][teal],[/teal]   [green][i]'third'[/i][/green] [teal]);[/teal]

Feherke.
 
Now I feel dumb. Didn't think to examine the value. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top