I got a file:

I wake     up in          the                                morning
What       time?
Always     wakeup      at                    6am.

what would be an easy way to determine the number of white spaces between each word?

Thanks in advance
David

Recommended Answers

All 2 Replies

#!/usr/bin/perl;
use strict;
use warnings;

while (<DATA>){
    my @whitespacegroups = m/\w(\s+)\w/g;
    foreach my $whitespacegroup(@whitespacegroups){
        my $count = length $whitespacegroup;
        print "$count spaces\t";
    }
    print "\n";
}
__DATA__
I wake     up in          the                                morning
What       time?
Always     wakeup      at                    6am.

Output is:

1 spaces	5 spaces	1 spaces	10 spaces	32 spaces	
7 spaces	
5 spaces	6 spaces	20 spaces

And another one way by using of expression statement

#!/usr/bin/perl;
use strict;
use warnings;

while (<DATA>){
	s/(\s+)/" [".length($1)." Spaces] "/ge;
	print ;
}
__DATA__
I wake     up in          the                                morning
What       time?
Always     wakeup      at                    6am.
commented: Good use for the -e option. +9
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.