Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

If I'm understanding you correctly (which I'm not at all sure), what you want is something like could be achieved with the following if/elsif... structure

sub lookup { my ($type, $ref) = @_; if ($type eq 'typeA') { return $ref->{key1}[3]{key2}; } elsif ($type eq 'typeB') { return $ref->{key3}; } elsif ($type eq 'typeC') { return $ref->[1]{key4}; } # ... }

i.e. with the following minimal sample data

my @LoANY = ( { type => "typeA", data => { key1 => [0,1,2, { key2 => "Value1" } ] } }, { type => "typeB", data => { key3 => "Value2" } }, { type => "typeC", data => [0, { key4 => " Value3" } ] }, # ... );

this loop

for my $elem (@LoANY) { print lookup($elem->{type}, $elem->{data}), "\n"; }

would print

Value1 Value2 Value3

But you think the if/elsif thingy is not perlish enough, would not scale decently up to a gazillion of different types, or some such... (?)

In that case, one other way to do it would be to set up little "accessor" functions (not in the OO sense, thus the quotes), which you would index via the hash, e.g.

my %map = ( typeA => sub { $_[0]->{key1}[3]{key2} }, typeB => sub { $_[0]->{key3} }, typeC => sub { $_[0]->[1]{key4} }, # ... );

In that case, you could write the above loop as

for my $elem (@LoANY) { print $map{$elem->{type}}->($elem->{data}), "\n"; }

The type would select the appropriate function (via %map), which "knows" how to get at the desired data. The data (i.e.the toplevel ref to some data structure) is passed to the function as argument.

I'm sure that once you confirm this is what you want to do, other Monks will come up with various other solutions... :)


In reply to Re: storing and using LoL path by almut
in thread storing and using LoL path by rootcho

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 scrutinizing the Monastery: (4)
As of 2024-04-24 02:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found