Hi mshefa,
I will advise that you use 3 argument open function as demonstrated below. Moreover, please check the proper usage of split function...
The following code show how to get desired result:
#!/usr/bin/perl
use warnings;
use strict;
my $parameter = qr/\.|"|,/;
my @words;
open my $fh, '<', "test.txt" or die " can't open file: $!";
while ( defined( my $line_to_consider = <$fh> ) ) {
my $new_word;
for ( split //, $line_to_consider ) {
push @words, $_ if /$parameter/;
if (/[-a-z]/i) {
$new_word .= $_;
}
else {
push @words, $new_word;
$new_word = qq{};
}
}
}
close $fh or die "can't close file: $!";
@words = grep { $_ ne "" } @words;
print $_," = 1 word\n" for @words;
Your Output is like thus:
He = 1 word
, = 1 word
said = 1 word
" = 1 word
. = 1 word
Well-done = 1 word
" = 1 word
Do perldoc -f split from thte CLI to see proper usage of split function in perl.
Hope this help.