Edit: I misunderstood the left/right requirement in my first pass. (Are Mulligans permissible with these teams?) Here's a version that's hopefully much closer to what you want:

my %players; for (<DATA>) { chomp; my ($team) = m!^ (.+?) = (?<left>.+?) \s*/\s* (?<right>.+) $!x or die "Line `$_' doesn't match"; for my $side (qw<left right>) { $players{$_}{$side} = $team for split m!\s*[,/]\s*!, $+{$side} +; } } # Remove players only on one team delete @players{ grep { keys %{$players{$_}} == 1 } keys %players }; local $" = ' AND '; print "$_ IS IN @{$players{$_}}{qw<left right>}\n" for keys %players;

The logic is relatively straightforward. For each line, parse the team, left, and right strings or die trying, and then split the left and right chunks into left/right. We are then left with, for example, $players{Don} = { left => 'Team4', right => 'Team5' };. After that, we can delete any player who has only 1 key in their hash, as they are only on left or right, and not both.

I'll leave my original misguided answer below the readmore tag.

As I understand it, your goal is to get a list of players who are assigned to more than one team, correct? The teams have a left/right side which I don't understand, but I would guess that a player can't be on the same team more than once, so checking whether they're on the team at all is sufficient? Here's my approach, which splits each line and puts every player on the team into the %players hash. Because you're only interested in duplicates, we then delete the players who are only on one team. Finally, a trivial loop to print the results.

Don's entry in the %players hash would look like $players{Don} = ['Team4', 'Team5'].

my %players; for (<DATA>) { chomp; my ($team,$players) = split /=/; push @{$players{$_}}, $team for split m!\s*[,/]\s*!, $players; } delete @players{ grep { @{$players{$_}} == 1 } keys %players }; local $" = ' AND '; print "$_ IS IN @{$players{$_}}\n" for keys %players;
use strict; use warnings; omitted for brevity.

In reply to Re: Matching problem by rjt
in thread Matching problem by jo26

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.