http://www.perlmonks.org?node_id=1078973

divitto has asked for the wisdom of the Perl Monks concerning the following question:

<SOLVED> Let me explain, I know that @$arrayRef will return the array that the reference is pointing to. I've also tried @{ $arrayRef } as well. i've tested both of these scenarios in a seperate script just to make sure they work on my machine and they do. (not suprisingly) When i try to run my actual script (below) i get a weird error, that has stumped me for the last two days. I could use some wisdom, Thank you in advance. here's the code:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; #protos sub mergeSort( \@ ); my @unsortedData = ( 5, 4, 7, 2, 1, 3, 6, 9, 8, 10 ); # show the original data to screen for( @unsortedData ) { print "$_ "; } print "\n"; my $sortedData = &mergeSort( \@unsortedData ); # show the new data to screen for( @$sortedData ) { print "$_ "; } print "\n"; ### dereferencing of arrays isnt working in below method sub mergeSort( \@arrayRef ) { my ( $data ) = @_; if( @$data < 2 ) { return $data; } # if there is less then two it +ems it's already sorted!! use integer; #split the array into two parts my $middle = @$data / 2; print "middle: $middle\n"; my @leftSide = (); print "LeftSide:\n"; for( my $i = 0; $i < $middle; $i++ ) { @leftSide[$i] = @$data[$i]; print "@leftSide[$i] "; } print "\n"; my @rightSide = (); print "RightSide:\n"; for( my $i = $middle; $i < @$data; $i++ ) { @rightSide[$i - $middle] = @$data[$i]; print "@rightSide[$i - $middle] "; } print "\n"; &mergeSort( @leftSide ); &mergeSort( @rightSide ); &merge( @leftSide, @rightSide, $data ); return $data; } sub merge { my ( @groupA, @groupB, $dataRef ) = @_; my $i = 0; my $j = 0; my $k = 0; while( $i < @groupA && $j < @groupB ) { if( @groupA[$i] <= @groupB[$j] ) { @$dataRef[$k] = @groupA[$i]; $i++; } else { @$dataRef[$k] = @groupB[$j]; $j++; } $k++; } while( $i < @groupA ) { @$dataRef[$k] = @groupA[$i]; $i++; $k++; } while( $j < @groupB ) { @$dataRef[$k] = @groupB[$j]; $j++; $k++; } }

I was trying to implement a merge sort. (i'm aware the default for perl's sort() is a merge sort. thats not the point) the problem comes from my mergeSort subroutine either when i test that the array is less then 2 or when i try to get the size of the array for my $middle calculation. the output i get is:

5 4 7 2 1 3 6 9 8 10 middle: 5 LeftSide: 5 4 7 2 1 RightSide: 3 6 9 8 10 Can't use string ("5") as an ARRAY ref while "strict refs" in use at m +ergeSort.pl line 34.

it's probably just me getting lost in context but i don't think im using a string as an array reference, any ideas?