#!/usr/bin/env perl use strict; use warnings; use List::MoreUtils qw/uniq/; use feature qw/state say/; my $friend_id = 1009; my %seen; my @teams; my @reps; while (<>) { chomp; state $count = 0; if($count > 0) { my $pair = make_pair($_); $seen{$pair->[0]}++; $seen{$pair->[1]}++; push @teams, $pair } $count++; } LINE: for my $pair (@teams) { my ($first, $second) = ($pair->[0], $pair->[1]); if($seen{$first} >= 2) { push @reps, $first; next LINE; } elsif($seen{$second} >= 2) { push @reps, $second; next LINE; } else { $second == 1009 ? push @reps, $second : $first == 1009 ? push @reps, $first : push @reps, $first; } } my @result = uniq @reps; say scalar @result; say for @result; sub make_pair { ## Str -> ArrayRef my $s = shift; my @pair = split / /, $s; \@pair; }