http://www.perlmonks.org?node_id=1059623


in reply to $1 does not reset to undef

As you noticed, $1 does not get set to undef on a failed match. The solution is to test the match:
use strict; use warnings; my @arry=( 'bond0.2 Link encap:Ethernet', 'bond4.3:6 Link encap:Ethernet', 'bond3 Link encap:Ethernet', 'bond5:0 Link encap:Ethernet', 'bond1.5:2 Link encap:Ethernet', 'bond2.6 Link encap:Ethernet' ); foreach (@arry) { if (/^([\w\.]+)\s+Link.*$/) { printf "Array item: %-30s\tmatch: %s\n",$_,$1; } }