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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.