#Getting all individual files and number of total files @ISFiles = glob 'dir1/file.split.*'; $ISNumFiles = scalar(@ISFiles); #getISCount() function that gets count for each file sub getISCount { my @files = @_; foreach $file (@files) { open(FILE, $file) or die "Can't open `$file': $!"; while (sysread FILE, $buffer, 4096) { $count += ($buffer =~ tr/\n//); } close FILE; } return $count; } $i = 0; $j = 1; while($i < scalar(@ISFiles)) { $ISCountForFile[$j] = &getISCount(@ISFiles[$i]); $i = $i + 1; $j = $j + 1; } $combinedCount = &getISCount(@ISFiles); print "combined count - $combinedCount\n";