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


In reply to Re: Newbie Q:How do I compare items within a string? by johngg
in thread Newbie Q:How do I compare items within a string? by PerlGrrl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.