in reply to Re: Sort multidimensional array by third item
in thread Sort multidimensional array by third item

Since @hold_array is outside the scope of your for loop, you are repeatedly pushing a reference to the same array to the end of your @item_array. An easier way to achieve your goal would be using anonymous arrays - see Making References in perlreftut. This might look like:

for my $line (@line_array) { push(@item_array, [split(/\s/, $line)]); }

If you want to use an explicit temporary storage array, you could use a variable that is scoped to the loop using my. This way, each iteration will create a new array with the same name, so each reference will be different.

for my $line (@line_array) { my @hold_array = split(/\s/, $line); push(@item_array, \@hold_array); }

Note I've swapped to Foreach Loops syntax to avoid possible indexing errors.

I also note you use our a lot. Each time you do that, you are creating a global variable. Is there some reason you want to do that? Likely, you should be using my instead.