Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Matching problem

by rjt (Curate)
on Sep 09, 2013 at 01:14 UTC ( [id://1052948]=note: print w/replies, xml ) Need Help??


in reply to Matching problem

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1052948]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found