Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Dereferencing arrays

by kennethk (Abbot)
on Mar 19, 2014 at 20:23 UTC ( #1078978=note: print w/replies, xml ) Need Help??

in reply to <SOLVED>Dereferencing arrays

One thing that jumps out at me is that the code issues a lot of warnings, which makes me think you aren't actually running with warnings in place. You use a lot of array slices (e.g. @leftSide[$i] in place of $leftSide[$i]) and you use a pointless and malformed prototype for mergeSort. You also use & syntax for your method call, which dodges the prototype anyway. Please make sure you post the code you are using, and don't just slap warnings on for a post.

When I run your code, the error you cite occurs on my line 30. It hits because when you call mergeSort on line 51, @leftSide array has 5 elements. Since you've used Prototypes, mergeSort expects a single scalar. However, you undermine the protoype system by invoking your method with &, so Perl stuffs your array in scalar context; thus the array length of 5. The general solution is don't use prototypes unless you actually know how to use them. If you change your code to read:

mergeSort( @leftSide ); mergeSort( @rightSide ); merge( @leftSide, @rightSide, $data );
You will solve this particular issue, though your sort will still fail for the reasons that BrowserUk hints at.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1078978]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-06-25 00:24 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.