my $marker = qr/^ NP \s+ U \s+ Pu $/x; my $columns = qr/^ (\d+) \s+ (\d+) \s+ (\d+) $/x; while (<>) { # If we find our line with the column names, if (m/$marker/) { # Read the following line to look for their numbers. $_ = <>; if (m/$columns/) { print "NP = $1, U = $2, Pu = $3\n"; } else { print "Line after NP/U/Pu doesn't give numbers.\n"; } } }