in reply to for loops and 'and'
Array slices are different. They don't have a scalar context per se; if scalar context is imposed (i.e. by being the first operand to ?:) the last element of the slice will be returned (and checked for truth by ?:). So assuming your desired-but-not-working-as-is-code was print for @a[@indices] or 1..11 you need to decide what you mean to do by checking @a[@indices] for truth. That could be any of a number of things. Perhaps one of these would be what you meant:
I.e. use the array if (any/all) of the elements are (true/defined/existing), otherwise fall back on 1..11.print for grep($a[$_], @indices) ? @a[@indices] : 1..11; print for grep(defined $a[$_], @indices) ? @a[@indices] : 1..11; print for grep(exists $a[$_], @indices) ? @a[@indices] : 1..11; print for !grep(!$a[$_], @indices) ? @a[@indices] : 1..11; print for !grep(!defined $a[$_], @indices) ? @a[@indices] : 1..11; print for !grep(!exists $a[$_], @indices) ? @a[@indices] : 1..11; # update: actually use slices after ?
|
|---|