Fletch has got at the heart of your problem. I'd add three things to his observation. I'm also confused by your XML. Will it ever contain more than one act? If so, how do you know when the act ends (since the ACT node seems to end with the title)? I'm going to assume here that you've got only one act (and one play) per XML file, and suggest something that might work. (My XPath is rusty, so take this with a healthy grain of salt.)
for my $scene ( $doc->findnodes('/PLAY/SCENE') ) { ITEM: for my $item ( $scene->findnodes('SPEECH') ) { my $speaker = $item->findvalue('SPEAKER'); next ITEM unless $speaker eq $searchterm; push @{$act{$scene}}, [ map { $_->to_literal } $item->findno +des('LINE') ]; } }
You could dump this with something like
for my $scene ( keys %act ) { print "SCENE $scene:\n"; print "$searchterm:\n", join "\n", @$_ for @{$act{$scene}}; }

In reply to Re: Setting up a hash in a linear search through XML by JadeNB
in thread Setting up a hash in a linear search through XML by Anonymous Monk

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.