#!/usr/bin/perl -w use strict; my $subnet = '192.168.87'; my %data = (); # precompile regex for performance.. my $regex = qr#^($subnet\.\d+)#; # read file line by line while (my $line = ) { chomp($line); if ($line =~ $regex) { $data{$1}++; } elsif ($debug) { print STDERR "Didn't match: $line\n"; } } #### my @matches = grep { m/$regex/ } @lines; ($_) = m/$regex/ foreach @matches;