When you write #arr_ref, Perl examines the array @arr_ref, which you never defined. Since it is not defined, its highest index is -1. The expression you intended is likely $#$arr_ref, which returns 7 as desired. The following code does what I assume you intended, with strict and warnings thrown in for good measure:Global symbol "@arr_ref" requires explicit package name at fluff.pl li +ne 12. Execution of fluff.pl aborted due to compilation errors.
#!/usr/bin/perl use strict; use warnings; my @arr_val=(1,2,3,4,5,6,7,8); my $arr_ref=\@arr_val; print $$arr_ref[-1]; print "\n"; print $$arr_ref[@$arr_ref-1]; print "\n"; print $#arr_val; print "\n"; print $#$arr_ref;
See perfreftut or perfref for more information on references, and see Use strict warnings and diagnostics or die for a howto and why for use strict; use warnings;.
In reply to Re: Print last element using perl reference
by kennethk
in thread Print last element using perl reference
by gobisankar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |