0

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

3
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by k_manimuthu
0
#!/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

Edited by d5e5: n/a

1

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.

Edited by k_manimuthu: n/a

Votes + Comments
Good use for the -e option.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.