in reply to pass a hash to subroutine
#! perl -w %a = (qw{ good orange good apple} ); TEST(%a); sub TEST{ my %h = @_; foreach (keys %h){ print $_; } }
Note using @_ where you had used $_[0] - that converts the entire array of parameters into a hash.
This is because the function receives a list of param, value, param, value.., which is stored in @_. By doing %h = @_; you put this into a hash with the first, third, fifth etc values as keys, and the second, fourth, sixth etc as values.
--------------------------------------------------------------
"If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."
John Brunner, "The Shockwave Rider".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: pass a hash to subroutine
by benlaw (Scribe) on Nov 07, 2005 at 14:29 UTC | |
by g0n (Priest) on Nov 07, 2005 at 14:49 UTC | |
by blazar (Canon) on Nov 08, 2005 at 08:07 UTC |