in reply to Counting instances of words
#! c:\perl\bin print("Enter filename to open (text only):\n\n");
No
use strict; use warnings;
Too bad!
$in=<STDIN>; chomp $in; if ($in !~ /\.txt$/i) { $in.=".txt"; }
chomp(my $in=<STDIN>); $in .= '.txt' unless $in =~ /\.txt$/i;
open ($in, "$in") or die("$in could not be opened:$!\n");
Too bad: not only it features useless quoting of variables, but it's also wrong in that for lexical filehandles to work, the variable must be undefined.
open my $infh, '<', $in or die "$in could not be opened:$!\n";
s/\band\b/$word/ig; while (/\band\b/ig) { ++$count; }
After you've substituted all occurrances of 'and' with $word, there won't be many remaining unless $word =~ /\band\b/i. Count around the substitution, instead.
|
|---|