hi there
I have the following script in which i have created a PrintHash() function.
I want to pass to this function the reference to a hash (in the final code i will be passing different hashes to this print function hence the need for a function). I am getting an error
Type of arg 1 to each must be hash (not string) at ./script.pl line 10, near ""$hash" ) "
Here is the script, I have highlighted the function in blue ....its not happy with me passing a string to the function instead of the variable that actually contains the hash (which i guess is fair enough), is there a way around this ?
PS: FYI this function works fine when not in a function and inserted straight into the body of the script
#!/usr/bin/perl -w
my $svhref;
my $serial = "0629AN1200";
sub PrintHash($)
{
my $hash = shift;
while ( my ( $key, $value ) = each "$hash" ) {
print $key, " ->\n";
while ( my ( $key, $value ) = each %$value ) {
print "\t", $key, " -> ", $value, "\n";
}
}
print "\n";
}
open(IN, "$serial.txt") or die "can't open $serial.txt $!\n";
while (<IN>)
{
chomp(); # remove line feed
my ($nic_name, $nic_speed, $nic_duplex, $nic_ip) = split(/\|/);
$svhref->{ $nic_name } = {
nic_speed => $nic_speed,
nic_duplex => $nic_duplex,
nic_ip => $nic_ip,
};
}
close(IN);
PrintHash('%$svhref');
---------- Post updated at 09:38 PM ---------- Previous update was at 09:04 PM ----------
Ok, answered my own question just after posting, please ignore (details of what i fixed for anyone who happens to be interested)
changed from this
while ( my ( $key, $value ) = each "$hash" ) {
to this
while ( my ( $key, $value ) = each %$hash ) {
changed from this
PrintHash('%$svhref');
to this
PrintHash($svhref);