#! /usr/bin/perl use strict ; use warnings ; use Data::Dumper ; my @match_arr = qw( count pick words ) ; my %match_hash = map { $_ => 1 } @match_arr ; my %word_count ; while ( ) { my @words = split ; for ( @words ) { $word_count{$_}++ if $match_hash{$_} } } print Dumper \%word_count ; exit ; __DATA__ This is my data. I will first count all the words herein, then pick and choose the data I want. I can pick the words using grep.