in reply to Newbie Q:How do I compare items within a string?

This is one way you could check for repeated words, counting them and also keeping track of the order. I have changed your split slightly to cope with comma and space next to each other and also with newlines. This solution also uses a hash keyed by the lowercase word and the value for each word is another hash with elements for number of occurrences and list of occurrence positions.

use strict; use warnings; use Data::Dumper; { local $/ = undef; $_ = <DATA>; } my @words = split /[., \n]+/; print "$_\n" for @words; my $rhWords = {}; my $order = 0; foreach my $word (@words) { my $lcWord = lc $word; push @{$rhWords->{$lcWord}->{order}}, ++ $order; $rhWords->{$lcWord}->{count} ++; } my $dd = Data::Dumper->new([$rhWords], [qw(rhWords)]); print $dd->Dumpxs(); __END__ The quick brown fox jumps over the lazy dog, the slow brown duck swims over the muddy pond. The chicken is pecking in the yard until the quic +k brown fox decides he is hungry.

When run this produces

The quick brown fox jumps over the lazy dog the slow brown duck swims over the muddy pond The chicken is pecking in the yard until the quick brown fox decides he is hungry $rhWords = { 'the' => { 'count' => 7, 'order' => [ 1, 7, 10, 16, 19, 24, 27 ] }, 'over' => { 'count' => 2, 'order' => [ 6, 15 ] }, 'is' => { 'count' => 2, 'order' => [ 21, 33 ] }, 'muddy' => { 'count' => 1, 'order' => [ 17 ] }, 'decides' => { 'count' => 1, 'order' => [ 31 ] }, 'swims' => { 'count' => 1, 'order' => [ 14 ] }, 'in' => { 'count' => 1, 'order' => [ 23 ] }, 'brown' => { 'count' => 3, 'order' => [ 3, 12, 29 ] }, 'chicken' => { 'count' => 1, 'order' => [ 20 ] }, 'he' => { 'count' => 1, 'order' => [ 32 ] }, 'pond' => { 'count' => 1, 'order' => [ 18 ] }, 'yard' => { 'count' => 1, 'order' => [ 25 ] }, 'jumps' => { 'count' => 1, 'order' => [ 5 ] }, 'duck' => { 'count' => 1, 'order' => [ 13 ] }, 'lazy' => { 'count' => 1, 'order' => [ 8 ] }, 'until' => { 'count' => 1, 'order' => [ 26 ] }, 'dog' => { 'count' => 1, 'order' => [ 9 ] }, 'fox' => { 'count' => 2, 'order' => [ 4, 30 ] }, 'pecking' => { 'count' => 1, 'order' => [ 22 ] }, 'quick' => { 'count' => 2, 'order' => [ 2, 28 ] }, 'hungry' => { 'count' => 1, 'order' => [ 34 ] }, 'slow' => { 'count' => 1, 'order' => [ 11 ] } };

I hope this is of use.

Cheers,

JohnGG