use warnings; use strict; use Data::Dumper; my %line; while () { 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.