Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Dereferencing arrays

by kennethk (Abbot)
on Mar 19, 2014 at 20:23 UTC ( [id://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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1078978]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found