in reply to Uninitialized value warning in subrutine
# Access directly the last element of the array sub queryDBI { my $query1 = "SELECT $_[2],$_[3],$_[-1] FROM " . $_[1]; return $query1; } print queryDBI(qw/first second third fourth fifth_and_last/),$/ x 2'
# In scalar context, @array returns the number of elements in contains +. # As @array indexes start at 0 (unless someone explicitly changes this + through $[), we substract 1 sub queryDBI { my $query1 = "SELECT $_[2],$_[3],$_[@_-1] FROM " . $_[1]; return $query1; } print queryDBI(qw/first second third fourth fifth_and_last/),$/ x 2'
For further info on predefined variables, as $_ and @_ check out perlvar# $#array returns the index of the last element of the array. sub queryDBI { my $query1 = "SELECT $_[2],$_[3],$_[$#_] FROM " . $_[1]; return $query1; } print queryDBI(qw/first second third fourth fifth_and_last/),$/ x 2'
|
|---|