How do I make an array shorter in Perl? I read some webpages indicating that I can assign:
$#ARRAY = 42;
I read that the use of $# is deprecated. I need a solution that will work for an array of arrays, too. This didn’t work:
$#$ARRAY[$i] = 42;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m not aware of assigning
$#ARRAYbeing deprecated;perldoc perldatafrom 5.10.0 certainly says nothing about it. It is the fastest way to truncate an array.If you want something a little more readable, use
splice:(Note
43instead of42–$#ARRAYgets you the last index of the array, whereassplicetaks the length of the array instead).As for working on arrays of arrays, I assume you mean being able to truncate a nested array via a reference? In that case, you want:
or