|We don't bite newbies here... much
Re^2: Setting variables inside a hash: newbie questionby hmerrill (Friar)
|on Nov 15, 2004 at 12:57 UTC
Ben is absolutely right - pass a hash *by reference* - that is, pass a reference to a hash instead of the hash itself.
One concept that Ben didn't mention that I think is worth acknowledging is that when parameters are passed to a subroutine, that argument list is "flattened" out into a list of scalars - arrays are flattened out into a list of scalars, and hashes are flattened out into a list of scalars - the result is one big list of scalars getting passed to the subroutine. This isn't a problem unless you pass an array or hash first, and *then* other parameter(s) - the array or hash that receives the 1st argument in the subroutine will likely "swallow up" all the arguments being passed in, since it can't tell from the one big "list" of scalars coming in which ones are intended for it and which ones are intended for other parameters.
To avoid confusion when passing "things" to subroutines, pass each parameter as a scalar - this means
Then if you really want to work with a "copy" of the array or hash (instead of just using the reference to refer to elements in the array or hash) whose reference was passed into the subroutine, you can create a copy like this: