You are too focused on the immediate solution and haven't provided enough context for us to test suggestions against. A bit of overview of the big picture problem can save a lot of iterations while we slowly leach enough context out of you to be helpful. Something like the following problem description could help a lot:
I have a schedule file with named lists of tasks. The task lists include actors involved in the tasks. I also have a file containing a list of actors I'm interested in, and a definitions file matching actors to the jobs they do. I want to generate an output list indicating which actors are used and, if they are used, which schedule they appear in.
Given that and assuming the sample data from my previous example the following fits the bill:
.
.
.
open my $schdIn, '<', \$scheduleData or die "Can't open 'Schedule': $!
+";
my %actorSchedules;
my $scheduleName;
while (defined(my $line = <$schdIn>)) {
if ($line =~ /SCHEDULE\s+(\S+)/) {
$scheduleName = $1;
next;
}
next if $line !~ /(\S+)#(\S+)/;
++$actorSchedules{$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 $tail = $actorSchedules{$1} ? 'Yes' : 'No';
my @schedules = keys %{$actorSchedules{$1}};
$tail = join ' | ', $tail, @schedules;
print "$defLine | $tail\n";
}
Prints:
Mindy#mu4 | Yes | Wanted
Orlon#mu5 | No
Nancy#mu6 | Yes | Unwanted
Harry#mu7 | No
Of course I don't know just what your big picture goal is so it's likely I haven't hit the mark exactly.
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
|