You said:
I have quite some nested loops that all need values from upper loops.

But in your code snippet, it seems like the only need you have for the values from upper loops is to use the hash keys and array index counters for navigating through the "doc" structure returned by XML::Simple.

If that is also the situation for your "actual" application, then the suggestion about about using recursion will make that a moot point: your recursive sub simply takes as a parameter the hash ref -- on the initial (outermost) call, this is the hash ref returned by XML::Simple::XMLin. On subsequent (recursing) calls, it's a hash ref at successively lower levels of the structure.

OTOH, if there are tag attributes at level 1, and you want to associate those with information that gets parsed at level 4 or whatever, you'll probably want your recursive sub to take additional parameters, so that info gleaned from an upper level can be passed to lower levels.


In reply to Re: Nested Loops vs Good programming by graff
in thread Nested Loops vs Good programming by Sporti69

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.