This looks like recursion to me.

sub devices_for { my ( $thing ) = @_; my @subs = query( $thing ); return map { /^DEVICE\d+/ ? $_ : devices_for( $_ ) } @subs; }

The first call, devices_for( 'GROUP1' ), causes a query( 'GROUP1' ). The map sends the device entries right through but causes a recursive call for 'GROUP2' and so forth. What the initial call for 'GROUP1' returns just a list of devices. Slip in a sort if you need them in order.

Update: Slightly better:

sub devices_for { my ( $thing ) = @_; return $thing if $thing =~ /^DEVICE\d/; return map { devices_for( $_ ) } query( $thing ); }

Or all in "one step":

sub devices_for { return ( $_[0] =~ /^DEVICE\d/ ) ? $_[0] : map { devices_for( $_ ) } query( $_[0] ); }

In reply to Re: Unknown levels of sub-element extraction by kyle
in thread Unknown levels of sub-element extraction by onegative

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.