Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.)


In reply to Re: creating a subroutine for accessing hash of arrays by tilly
in thread creating a subroutine for accessing hash of arrays by jonnyfolk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found