Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Using foreach on a hash passed to a subroutine

by carcassonne (Pilgrim)
on Sep 13, 2009 at 15:03 UTC ( [id://794992]=perlquestion: print w/replies, xml ) Need Help??

carcassonne has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I'm stumped by what seems to be a very basic thing. Using a hash ref inside a subroutine. I've tried several syntaxes but none worked so far, with results ranging from syntax errors to 'not a HASH reference' and such.

Here is the problem in a nutshell.

The idea is to be able to work on different parts of a large hash whose some of its 'subhashes' so to speak have the same structure. The result of that work is placed inside a hash taht contains already unit data, passed along to the subroutine. The work has to do with the interface sections of different types of units.

my %myUnitIfs; processInterfaceData(\$config{units}{myUnit}{interfaces}, \%myUnitIfs) +;

This would eventually be called on other units, with diffrenet new hashes to collect output:

processInterfaceData(\$config{units}{yourUnit}{interfaces}, \%yourUnit +Ifs); processInterfaceData(\$config{units}{anotherUnit}{interfaces}, \%anoth +erUnitIfs);

And here I've tried: $h, %$h, and many other variations.

sub processInterfaceData { my ($h, $nh) = @_; print "$key\n" foreach my $key (keys %{$h}); }

Pray tell, how one is to write the reference inside the subroutine so that elements of the hash are properly accessed ? - Thanks !

Replies are listed 'Best First'.
Re: Using foreach on a hash passed to a subroutine
by Jenda (Abbot) on Sep 13, 2009 at 15:25 UTC

    Assuming $config{units}{yourUnit}{interfaces} is a reference to a hash, then \$config{units}{yourUnit}{interfaces} is a reference to a reference to a hash. So you'd have to use ...(keys %{$$h}). I guess you want to drop the backslash.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Thanks all. As pointed out the problem was not in the subroutine but in the nature of the passed parameter. Thanks also for the examples.
Re: Using foreach on a hash passed to a subroutine
by bart (Canon) on Sep 13, 2009 at 15:24 UTC
    A hash of hashes already contains hash references, so you probably just have to pass $config{units}{myUnit}{interfaces}. Because now, it looks like you're actually passing a scalar reference to a scalar containing a hash reference.
Re: Using foreach on a hash passed to a subroutine
by Anonymous Monk on Sep 13, 2009 at 15:24 UTC
    References quick reference
    #!/usr/bin/perl -- use strict; use warnings; use Data::Dumper; my %one = ( a => { b => { c => { d => { 6 } } } } ); print Dumper( \%one, $one{a}{b}{c} ); my $none = $one{a}{b}{c} ; for my $k( keys %$none ){ $none->{"$k$k"} = "$k$k"; } print Dumper( \%one); __END__ $VAR1 = { 'a' => { 'b' => { 'c' => { 'd' => { '6' => undef } } } } }; $VAR2 = $VAR1->{'a'}{'b'}{'c'}; $VAR1 = { 'a' => { 'b' => { 'c' => { 'dd' => 'dd', 'd' => { '6' => undef } } } } };
Re: Using foreach on a hash passed to a subroutine
by cdarke (Prior) on Sep 14, 2009 at 08:52 UTC
    If you are offended by subroutine prototypes then look-away now.

    This is an occasion where a prototype would have helped (OK, so there aren't that many of them):
    Update: Doh! No it wouldn't, sorry.
    use strict; use warnings; sub processInterfaceData (\%\%) { my ($h, $nh) = @_; foreach my $key (keys %{$h}) { print "$key\n"; } } my %myUnitIfs; my %config; processInterfaceData($config{units}{myUnit}{interfaces}, %myUnitIfs);
    Gives:
    Type of arg 1 to main::processInterfaceData must be hash (not hash ele +ment)
    It gives the error as the calling line, not the subroutine, at compile time. Alos, because the caller does not specify the \ to indicate the reference, it is less likely for the problem to arise.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://794992]
Approved by ww
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found