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

I have several statements accessing a hash of arrays, eg:
while ( (my $key, my $value) = each %partners) { if ($item eq $key) { ($partners,$email,$nickname,$realname,$postcode,$phone) = +(@$value); push (@allmembers, $item); my @partners_split = split / /, $partners; push (@allmembers, @partners_split); } }
while ( (my $key, my $value) = each %partners) { if ($item eq $key) { ($partners,$email,$nickname,$realname,$postcode,$phone) = (@$v +alue); ##another, different action; } }
These are identical apart from the action that takes place within, and so I would like to create a subroutine containing a variable representing the action, which I can define before calling the subroutine.

Could anyone advise me how I could do this?

Update: I noticed that the actions in both examples were very similar so I changed the second to a comment to avoid confusion...

Replies are listed 'Best First'.
Re: creating a subroutine for accessing hash of arrays
by tilly (Archbishop) on Mar 27, 2004 at 06:53 UTC
    A better simplification is to stop writing extra loops.
    if (exists $partners{$item}) { my ($partners, $email, $nickname, $realname, $postcode, $phone) = @{ +$partners{$item}}; # Do something. }
    That not only is conceptually simpler, but it is much more efficient. Hashes are good for looking things up by name, use them that way.

    I'd also question why you have stuff that you really want to access by name being accessed by position in an array. It looks to me like you want a hash of hashes. Then your snippet above would become:

    if (exists $partners{$item}) { push @allmembers, $item, split / /, $partners{$item}{partners}; }
    At which point you probably don't need to be bothered about abstracting the control structure.

    But for completeness, I'll answer your original question. And I like talking about the technique, so I'll provide it. But I really don't recommend that you do it at this point. Focus on the basics.

    The strategy is to create an anonymous function for the action. And then call it. Like this:

    sub do_for_partners { my ($item, $action) = @_; if (exists $partners{$item}) { $action->($item, $partners{$item}); } } # And the above example. do_for_partners($item, sub { push @allmembers, shift; push @allmembers, (shift)->{partners}; });
    But as I point out, the original control structure is simple enough that you don't really gain anything by adding this conceptual complexity. So I don't advise adding action at a distance here.

    (Note: All code is untested.)

      Great reply, tilly. Thanks very much. I have already put into action the hash of arrays solution and I shall look at implementing hash of hashes when I get my head around it. I was also pleased that you answered the original question tho' I do understand why I shouldn't use it in this case.

      I must confess that though I have read up on this sort of thing in the past and it all seemed to make sense, it didn't really click back then. Now that I have an implementation of my own to do I feel I can really make some progress with it. Cheers

Re: creating a subroutine for accessing hash of arrays
by etcshadow (Priest) on Mar 27, 2004 at 07:03 UTC
    If you want to have a subroutine apply some kind of "action" for you, to several things (internal to that subroutine), then you can use a subroutine reference. Example:
    while ( (my $key, my $value) = each %partners) { if ($item eq $key) { ($partners,$email,$nickname,$realname,$postcode,$phone) = (@$v +alue); push (@allmembers, $item); my @partners_split = split / /, $partners; push (@allmembers, @partners_split); } }
    sub do_thing { my ($partners,$item,$action) = @_; while ( (my $key, my $value) = each %$partners) { if ($item eq $key) { $action->($key,$value) } } } do_thing(\%partners,$item,sub { my ($key,$value) = @_; ($partners,$email,$nickname,$realname,$postcode,$phone) = (@$value +); push (@allmembers, $item); my @partners_split = split / /, $partners; push (@allmembers, @partners_split); }); # and then, you can do other things: do_thing(\%partners,$item,sub { my ($key,$value) = @_; # do other stuff with %partners, $item, $key, $value, etc });
    That's the general idea, at least... that you would create an anonomus subref and pass it into your subroutine as one of the parameters. Think: map or grep.

    On an additional note... I hope this code is just a thrown-together (not well-thought) example, as doing something like:

    while ( (my $key, my $value) = each %partners) { if ($item eq $key) { ... } }
    Is really just silly. You'd be much better off just doing stuff to $partners{$item}.
    ------------ :Wq Not an editor command: Wq
Re: creating a subroutine for accessing hash of arrays
by cLive ;-) (Prior) on Mar 27, 2004 at 06:55 UTC
    You don't really need a sub. Just simplify a little:
    ($partners,$email,$nickname,$realname,$postcode,$phone) = @{ $partners +{$item} };
    cLive ;-)