perl equivalent to grep -c

Guess the subject lines says it all.

What is the perl equivalent to grep -c

-c, --count
Suppress normal output; instead print a count of match-
ing lines for each input file. With the -v, --invert-
match option (see below), count non-matching lines.

So if the file name is somefile, my search pattern is houseboat and there are 3 occurences/instances of houseboat in the file, I want to

$var=houseboat
$x=`grep -c $var somefile`
print '$var $x\n"

output

houseboat 3

Thanks

perl -lne '$count++ if m/$ARGV[0]/o; END { print $ARGV[0], $count }'

See also perlfaq4 - perldoc.perl.org

Humm Still dont know why I dont see this ..

Source file
----------
car
boat
house

Result file
---------
house
camper
car
motorcycle
car
skateboard
car
airplane
boat
house
boat
blimp
house
train

Result of script
---------------
house 3
car 3
boat 2

So ..

open (FILE1, "SOURCEFILE.TXT") or die; #open the source file
open (FILE2, "RESULTFILE.TXT") or die; #open the results file
#
@RESULTARRAY = <FILE2>; #put result file content in an
#array
#foreach $lineinthesourcefile (<FILE1>) { #Step through the source file.
#/$linethesourcefile/g) {
# $count++; # count the number of lineinthesourcefile in
@RESULTARRARY, then print line and count.
}

open(FILE1,"<file");
open(FILE2,"<file1");
@file1 = <FILE1>;
%hashes=();
foreach my $i (@file1) {
 chomp($i) ;
 $hashes{$i}=0
}
while ( <FILE2> ) {
 chomp($_);  
 $hashes{$_}++;
} 
while ( ($key, $value) = each %hashes) {
    print "$key = $value\n";
}


You don't really need the array for @file1 then, you can just run another while loop over that and save the memory for the array which only got used during initialization of the hash.

You also don't need to chomp the values, as long as you are consistent on whether or not you chomp.

This assumes you actually want to simulate "grep -x" (match whole lines), not find a token anywhere on a line in file2. If that's what you actually want, maybe something like this would work.

open (FILE1, "SOURCEFILE.TXT") or die "$0: Could not open SOURCEFILE.TXT: $!\n";
open (FILE2, "RESULTFILE.TXT") or die "$0: Could not open RESULTFILE.TXT: $!\n";

@RESULTARRAY = <FILE2>;
foreach $lineinthesourcefile (<FILE1>) {
  chomp $lineinthesourcefile;  # trim trailing newline
  @matches = grep { /$lineinthesourcefile/ } @RESULTARRAY;
  print $lineinthesourcefile, " ", scalar @matches, "\n" if @matches;
}

The "grep" finds the elements in @RESULTARRAY matching the condition in { } and returns those as a list. If that list has any elements, we print a report. (scalar @matches returns how many elements @matches has.)

This matches regardless of context, so "car" will be matched inside another word, like "scars" or "career". You can dress up the regular expression in the grep some if you want different behavior.

I was tempted to use @INSANELYLONGUPPERCASEVARIABLENAME instead of @matches, but if that's what you want, that's obviously easy to fix. <ducks>

Ok fellas .. first thanks for the help ... it all definitely moved me in the right direction

The question : How many times do the lines shown in Source file show up in the Result file

Source file
-----------
car
boat
house
girl
hate

Result file
----------
car
boy
girl
dog
car
love
hate
boat
boat
hate
boat
boat
boy
girl
dog

Script results :
---------------

1 lines in the result file contain: car
4 lines in the result file contain: boat
0 lines in the result file contain: house
2 lines in the result file contain: girl
2 lines in the result file contain: hate

Code - one thing I need to look at ... the break out is brutal :stuck_out_tongue:

open (FILE1, "cv1.TXT") or die "$0: Could not open SOURCEFILE.TXT: $!\n";
open (FILE2, "cv2.TXT") or die "$0: Could not open RESULTFILE.TXT: $!\n";

chomp(my @strings = <FILE2>);
while (1) {
foreach $pattern (<FILE1>) {
chomp($pattern);
#last if $pattern =~ /^\s*$/;
my @matches = eval {
grep /$pattern/, @strings;
};
if ($@) {
print "Error: $@";
} else {
my $count = @matches;
print "$count lines in the result file contain: $pattern \n"
}
}
exit;
}

If you want to know which lines in one file exist in the other, doesn't ghostdog74's solution work for you? If you want to know how many lines in file 2 contain one of the words (anywhere on a line) in file 1, the script I posted ought to work. So please clarify: which one is it?

This distinction may seem academic, but has implications for whether to look for equality (line equals string) or pattern matching (line contains pattern) and, to a lesser extent, whether or not the final newline on every line is significant. If your real-world application handles lots of data, it may also matter that equality is faster than pattern matching.

When posting code, it's much more legible if you wrap it in [CODE] tags.

Your use of eval seems somewhat weird, you usually don't need to trap bare Perl code within eval, it's more useful when invoking a system call or otherwise interacting with the outside world. Also what's the endless loop for?