Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Re: Re: Depth First Search and Boggle

by the_slycer (Chaplain)
on Aug 15, 2002 at 06:30 UTC ( [id://190320]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Depth First Search and Boggle
in thread Depth First Search and Boggle

Here's a hint.
print "'$_'\n" foreach @_ at the start of your sub (before the assignment).
See what is happening here? You will get a printout like:
'0'
'0'
''
hmm.. not quite what you wanted I think. You are only passing 3 defined values into the sub. Two 0's and one "". Because your hash is undefined, it doesn't show up in the @_ list.

Just looking at this bit of code, it looks like you are trying to do the "right" thing by use'ing strict and passing your variables around to your subroutine. The thing is, if your variables have no value, then there is no reason to pass them. In fact, looking at it,there is absolutely no reason to define %visited or $word in the "main" code at all

In this bit of code, I would personally work more like
... dfs(0,0); ... sub dfs { my ($x,$y,$word,%visited) = @_; #no error here! ... process code ... dfs($x,$y,$word,%visited); #recursion call }
See the differences? First, because we only pass 2 defined values in the first call to dfs, we only assign to $x and $y. This is fine, because we don't use $word or %visited before we assign to them. Hence, we aren't using an undef'd variable. Nor are we assigning "" to the hash. Second, in subsequent calls, we pass the hash last

BrowserUk is completely right in this. Using an assignment like in your code, you will almost always end up with an odd value assignment. You could almost call an assignment to a hash (or an array for that matter) greedy, because it will always take up the "rest" of @_, if you need to pass variables and a hash or an array, always put the hash/array last in the assignment list. If you need to pass two lists (eg 2 arrays, or a hash and an array) you MUST pass by reference.

Log In?
Username:
Password:

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

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

    No recent polls found