perl - passing hash references to functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl - passing hash references to functions
# 1  
Old 09-01-2009
perl - passing hash references to functions

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

Code:
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

Code:
#!/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
Code:
while ( my ( $key, $value ) = each "$hash" ) {

to this
Code:
 while ( my ( $key, $value ) = each %$hash ) {


changed from this
Code:
PrintHash('%$svhref');

to this
Code:
PrintHash($svhref);

# 2  
Old 09-01-2009
You can also use Data Dumper..

Code:
use Data::Dumper;
print Dumper(\%svhref);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

2. Shell Programming and Scripting

Query related to references in array in Perl

Hi All I have a doubt and want to be cleared I am using @array = (10, 20); $rarray = \@array; #print "$rarray\n"; #print "@$rarray\n"; $rr= \$array; #print $$rr; $rr++; print $$rr; As you can see the $rr contains the reference to the first element of the array , now as the... (5 Replies)
Discussion started by: parthmittal2007
5 Replies

3. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

4. UNIX for Advanced & Expert Users

Passing Hash variable in to sql query in perl

Hi Everyone, Can anyone help me how do i call hash variable in to sql query in perl. Please see the script below i have defined two Hash %lc and %tab as below $lc{'REFF'}='V_RES_CLASS'; $lc{'CALE'}='V_CAP_CLASS'; $lc{'XRPD'}='V_XFMR_CLASS'; $tab{'V_RES_CLASS'}='V_MFR_SERS';... (6 Replies)
Discussion started by: jam_prasanna
6 Replies

5. Shell Programming and Scripting

Perl References/Dereferences

Can someone explain where can we actually used print $var->; or print $$var When does the -> becomes necessary and when its optional. (1 Reply)
Discussion started by: dinjo_jo
1 Replies

6. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

7. Shell Programming and Scripting

passing a hash to another script in perl

I have a script (say script1.sh ) and I am calling a script (say script2.sh) within the script1.sh. Here in script1.sh I have a hash ( say %hash1) and i have to pass this hash to script2.sh. Basically i have to do some processing in Scirpt2.sh based on the hash(key,values). I wanted to know how can... (2 Replies)
Discussion started by: ammu
2 Replies

8. Shell Programming and Scripting

Passing arrays between functions

Hi, I have a function that hold 3 arrayies. I need to pass them to another function as an input, for further use Could you please explain how to do that. Thanks (5 Replies)
Discussion started by: yoavbe
5 Replies

9. Shell Programming and Scripting

passing command line parameters to functions - sh

All, I have a sh script of the following tune: function a () { #functionality.. } function b () { #functionnlity.. } function check () { # this function checks for env and if all fine call build } function usage () { #sh usage details } function build () { #calls either a or b or... (5 Replies)
Discussion started by: vino
5 Replies

10. UNIX for Dummies Questions & Answers

Passing Hash Tables to Subroutines

Hi: How do I pass a hash table down to a subroutine along with some other variables? For example, I have say a subroutine play_with_hash: sub play_with_hash { my( $var1, $var2, %my_hash ) = @_; #do stuff with %my_hash ........... } Then I want to call the subroutine... (1 Reply)
Discussion started by: mirzabhai
1 Replies
Login or Register to Ask a Question