So something like this:

use warnings; use strict; my $scheduleData = <<SDATA; SCHEDULE Unwanted mu1 mu2 Nancy#mu3 mu4 mu2 END SCHEDULE Wanted mu1 mu2 Mindy#mu4 mu4 mu2 END SDATA my $agentData = <<ADATA; Harry Mindy Nancy Orlon ADATA my $defsData = <<DDATA; Mindy#mu4 SCRIPTNAME "doit.cmd" DESCRIPTION "mu2" Orlon#mu5 SCRIPTNAME "doit.cmd" DESCRIPTION "SOMETIMES HAS AGENTNAME Orlon" Nancy#mu6 SCRIPTNAME "doit.cmd" DESCRIPTION "mu2" Harry#mu7 SCRIPTNAME "SOMETIMES HAS AGENTNAME Harry" DESCRIPTION "mu2" DDATA open my $schdIn, '<', \$scheduleData or die "Can't open 'Schedule': $! +"; my %schedules; my $scheduleName; while (defined(my $line = <$schdIn>)) { if ($line =~ /SCHEDULE\s+(\S+)/) { $scheduleName = $1; next; } next if $line !~ /(\S+)#(\S+)/; ++$schedules{$1}{$scheduleName}; } open my $agentsIn, '<', \$agentData or die "Can't open 'Agents': $!"; my $agentsList = join '|', map {chomp; qr/\Q$_\E/} <$agentsIn>; my $agentsMatch = qr|\b($agentsList)\b|; my $wantedSchedule = 'Wanted'; open my $defsIn, '<', \$defsData or die "Can't open 'Definitions': $!" +; while (defined(my $defLine = <$defsIn>)) { chomp $defLine; next if $defLine !~ /$agentsMatch#/; my $lineout = $defLine; my $flag = $schedules{$1} ? 'Yes' : 'No'; my $wanted = $schedules{$1}{$wantedSchedule}; $wanted = $wanted ? " | $wantedSchedule" : ''; print "$defLine | $flag$wanted\n"; }

Prints:

Mindy#mu4 | Yes | Wanted Orlon#mu5 | No Nancy#mu6 | Yes Harry#mu7 | No

My eyes don't parse a wall of uppercase text so I changed strings to something I could work with. I also removed the irrelevant file opens and the code building %scheduleMatch from an external file. It is important to trim out needless code and errors to focus just on the task at hand (see I know what I mean. Why don't you?).

The key is to improve the parsing of the schedules data so that we have both the agent name and the schedule name together in the schedules hash. Then the test in the loop processing the defs data becomes trivial.

Update: remove duplicated lines at the start of the example code (bogus copy and paste :-( ).

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

In reply to Re^5: nesting loops help? by GrandFather
in thread nesting loops help? by shadowfox

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.