How do I modify the split function to consider the punctuations as words?

He = 1 word.
said = 1 word
, = 1 word
" = 1 word
Well-done = 1 word
. = 1 word
" = 1 word

Contents of the test file:

He said, "Well-done."

#!/usr/bin/perl -w

open  (FILE, "test.txt") or die "Error!";

my $words = 0;

while (<FILE>) 

    { 

        $words += scalar(split(/.+/));

    }

print (" words = $words \n ");

Recommended Answers

All 2 Replies

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.

commented: Great! +0

Thanks a lot for your help. I am new to perl. Will go ahead and do more research and practice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.