Perl is pretty nice about default values:
: jmglov@laurana; perl -e \'@foo; printf \"%d\\n\", $foo[123]\' 0 : jmglov@laurana; perl -e \'%foo; printf \"%d\\n\
Another approach would be overriding the Array#[] method and return the default value if there is no item
Array#[]
class Array def [](index) self.at(index) ? self.at(index) : 0 end end
and
arr = [1,2,3] puts arr[0] # print 1 puts arr[5] # print 0