in reply to Re^4: nesting loops help?
in thread nesting loops help?
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 :-( ).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: nesting loops help?
by shadowfox (Beadle) on Mar 14, 2022 at 02:52 UTC | |
|
Re^6: nesting loops help?
by shadowfox (Beadle) on Mar 14, 2022 at 15:25 UTC | |
by GrandFather (Saint) on Mar 14, 2022 at 20:56 UTC | |
by shadowfox (Beadle) on Mar 14, 2022 at 22:21 UTC | |
by GrandFather (Saint) on Mar 15, 2022 at 00:02 UTC | |
by shadowfox (Beadle) on Mar 15, 2022 at 02:12 UTC | |
|