<?xml version="1.0" encoding="windows-1252"?>
<node id="1005049" title="Re: separate word count for each line of a file" created="2012-11-21 22:54:16" updated="2012-11-21 22:54:16">
<type id="11">
note</type>
<author id="918402">
2teez</author>
<data>
<field name="doctext">
&lt;p&gt;
 Hi [melb100],&lt;br&gt;&lt;br&gt;
 I think you are really not far away from the solution you "dearly" seek.&lt;br&gt; I will rather use a Perl Hash, to get at the answers, while going over each of the line in a given file.&lt;br&gt; As shown below, A foot in the door, would give an entrance to the whole solution, I believe.&lt;br&gt;Like this:
&lt;c&gt;
use warnings;
use strict;
use Data::Dumper;

my %line;
while (&lt;DATA&gt;) {
    chomp;    # not chop

    # initialize total number of character to get for each line
    my $line_total_characters_without_space = 0;

    foreach my $w (split) {
        $line{'Total_number_of_words'}++;
        print " In line: ", $., " word: ", $w, " has length: ", length($w), $/;
        $line_total_characters_without_space += length($w);
    }

    # get the number of characters for each line, space inclusive
    push @{ $line{$.}{'line_total_characters_with_space'} }, length($_);

    # get the number of characters for each line, without including space
    push @{ $line{"line $."} }, $line_total_characters_without_space;
}
$Data::Dumper::Sortkeys = 1;
print Dumper \%line;

__DATA__
Mary had a little lamb,
whose fleece was white as snow.

And everywhere that Mary went,
the lamb was sure to go.

It followed her to school one day
which was against the rules.

&lt;/c&gt;
&lt;tt&gt;Output&lt;/tt&gt;
&lt;c&gt;
 In line: 1 word: Mary has length: 4
 In line: 1 word: had has length: 3
 In line: 1 word: a has length: 1
 In line: 1 word: little has length: 6
 In line: 1 word: lamb, has length: 5
 In line: 2 word: whose has length: 5
 In line: 2 word: fleece has length: 6
 In line: 2 word: was has length: 3
 In line: 2 word: white has length: 5
 In line: 2 word: as has length: 2
 In line: 2 word: snow. has length: 5
 In line: 4 word: And has length: 3
 In line: 4 word: everywhere has length: 10
 In line: 4 word: that has length: 4
 In line: 4 word: Mary has length: 4
 In line: 4 word: went, has length: 5
 In line: 5 word: the has length: 3
 In line: 5 word: lamb has length: 4
 In line: 5 word: was has length: 3
 In line: 5 word: sure has length: 4
 In line: 5 word: to has length: 2
 In line: 5 word: go. has length: 3
 In line: 7 word: It has length: 2
 In line: 7 word: followed has length: 8
 In line: 7 word: her has length: 3
 In line: 7 word: to has length: 2
 In line: 7 word: school has length: 6
 In line: 7 word: one has length: 3
 In line: 7 word: day has length: 3
 In line: 8 word: which has length: 5
 In line: 8 word: was has length: 3
 In line: 8 word: against has length: 7
 In line: 8 word: the has length: 3
 In line: 8 word: rules. has length: 6
$VAR1 = {
          '1' =&gt; {
                   'line_total_characters_with_space' =&gt; [
                                                           23
                                                         ]
                 },
          '2' =&gt; {
                   'line_total_characters_with_space' =&gt; [
                                                           31
                                                         ]
                 },
          '3' =&gt; {
                   'line_total_characters_with_space' =&gt; [
                                                           0
                                                         ]
                 },
          '4' =&gt; {
                   'line_total_characters_with_space' =&gt; [
                                                           30
                                                         ]
                 },
          '5' =&gt; {
                   'line_total_characters_with_space' =&gt; [
                                                           24
                                                         ]
                 },
          '6' =&gt; {
                   'line_total_characters_with_space' =&gt; [
                                                           0
                                                         ]
                 },
          '7' =&gt; {
                   'line_total_characters_with_space' =&gt; [
                                                           33
                                                         ]
                 },
          '8' =&gt; {
                   'line_total_characters_with_space' =&gt; [
                                                           28
                                                         ]
                 },
          'Total_number_of_words' =&gt; 34,
          'line 1' =&gt; [
                        19
                      ],
          'line 2' =&gt; [
                        26
                      ],
          'line 3' =&gt; [
                        0
                      ],
          'line 4' =&gt; [
                        26
                      ],
          'line 5' =&gt; [
                        19
                      ],
          'line 6' =&gt; [
                        0
                      ],
          'line 7' =&gt; [
                        27
                      ],
          'line 8' =&gt; [
                        24
                      ]
        };

&lt;/c&gt;
&lt;/p&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-918402"&gt;
If you tell me, I'll forget.&lt;br&gt;
If you show me, I'll remember.&lt;br&gt;
if you involve me, I'll understand.&lt;br&gt;
--- Author [unknown to me]
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1004978</field>
<field name="parent_node">
1004978</field>
</data>
</node>
