Perl: Array of Arrays, when do I need to cast

Cave of Programming gave me the hint how to cast:

“We can use this reference with push, pop, grep and so on if we first cast the reference to an actual array. To cast a reference to an array in Perl, surround the reference with {} brackets and prefix it with the array symbol @.” And: “The {} brackets around the cast are only necessary because of the [] index brackets on the end of the array name. If we just had a simple reference by itself, we could have simply stuck a ‘@’ onto the start of it.”

Source example looks like this:

# Append to the first array
# We need to typecast the reference to an 
# array before using push.
push @{$stuff[0]}, 'kiwi';

Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *