1.11M Members

perl split

 
0
 

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 ");
 
1
 

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.

 
0
 

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

Question Answered as of 1 Year Ago by 2teez
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article