Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How to use pass and return hashes in a subroutine

by chariscomp (Initiate)
on Aug 21, 2000 at 23:36 UTC ( [id://28894]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to write a subroutine that is to be passed a hash of items to be printed out. I am currently using something like this:
sub loop_print2 {

	local(%arg=$_[0],$key);
	foreach $key (sort keys %arg) {
		print "$key:\t$arg{$key}\n";
	}
}

I am calling it like this &loop_print2(%c); where %c is a hash that is already populated from a previous function call. I am also trying to find out how to return a hash from a function. Can I build it up and then use something like:
return %c;
at the end of the subroutine? Thanks for any help on this!
  • Comment on How to use pass and return hashes in a subroutine

Replies are listed 'Best First'.
Re: How to use pass and return hashes in a subroutine
by chromatic (Archbishop) on Aug 21, 2000 at 23:43 UTC
    I like to use references to the hash, but you can always just pass it to and from straight.

    Perl will flatten your hash into a normal list. Luckily, if you're expecting this, you can capture it in a normal hash again.

    my %values = ( name => 'chromatic', rank => 'saint', style => 'obnoxious', ); my %rev_values = do_something(%values); foreach my $key (keys %rev_values) { print "$key => $rev_values{$key}\n"; } sub do_something { my %values = @_; foreach my $key (keys %values) { print "$key => $values{$key}\n"; $values{$key} = reverse $values{$key}; } return %values; }
    A reference is conceptually more complicated, but it solves issues like parameter positioning and the trickiness involved in converting and copying hashes:
    my %values = ( foo => 1, bar => 2, baz => 3, ); my %rev_values = do_something(\%values); sub do_something { my $h_ref = shift; my %backwards; foreach my $key (keys %$h_ref) { print "$key => ", $h_ref->{$key}, "\n"; $backwards{$h_ref->{$key}} = $key; } return \%backwards; }
RE: How to use pass and return hashes in a subroutine
by KM (Priest) on Aug 21, 2000 at 23:47 UTC
    I'm not completely sure what you are asking. What errors are you getting? I also like to pass a hash ref to subs when I can, like foo(\%hash); And yes, you can return a hash like:

    return %foo; # my %hash = sub(\%hash); return $foo; # my $hash_ref = sub(\%hash);
    Tell us what you have done, and is not working. I also suggest using 'my' and not 'local'. And redoing that part to be like:

    my %arg = @_; my $key;

    Cheers,
    KM

Re: How to use pass and return hashes in a subroutine
by le (Friar) on Aug 21, 2000 at 23:50 UTC
    If you pass a hash-ref to your sub, it would look like this:
    &loop_print(\%c); sub loop_print { my $href = shift; foreach (keys %{$href}) { print "$_: ${$href}{$_}\n"; } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 22:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found