Welcome to the Monastery | |
PerlMonks |
Re: Re: Re: Depth First Search and Boggleby the_slycer (Chaplain) |
on Aug 15, 2002 at 06:30 UTC ( [id://190320]=note: print w/replies, xml ) | Need Help?? |
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 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.
In Section
Seekers of Perl Wisdom
|
|