#!/usr/bin/perl use v5.35.0; my $matches = 0; my $towels; while (<>) { chomp; next if /^$/; if (defined $towels) { $matches += /$towels/o; } else { $towels = '^(?:' . s/, /|/gr . ')+$'; } } say $matches;