use strict; use warnings; use Data::Dumper; my @slot; my $dpdrivloc = "MSL6000 Trinity"; my $regex = qr/\Q$dpdrivloc\E:\s*(\d+)/; # ... open DPSLOTFILE ... while ( ) { if (my ($numb) = $_ =~ $regex) { push @slot, $numb; } } print Dumper \@slot;