Hello all,

First of all thank you for your help. I use strict in my script, but this doesn't appear here.

I have a text file with sentences, one per line. A perl script then extracts each sentence and stores them in a temporary array (@temp). Then I use the following code to extract the duplicates.

@duplicates = grep $seen{$_}++, @temp; # here we count how many times each duplicate appears %seen = (); foreach my $item (@duplicates) { $seen{$item}++; } @unique_duplicates = keys %seen;

Everything works fine.

Now I'd like to make my code object-oriented. The script basically reads each sentence and create a new object:

$record=Entry->new(); $record->id(1); $record->duplicate(0); $record->src("This is a duplicate."); push @records, $record;

Here we have additionnally an ID, a flag 'duplicate' set to 0 and the sentence.

I push the $record to an array for later use. And this is basically where my problems start. I'm used to work with arrays and hashes, but here we have each array element being a hash:

print @records

gives you

Entry=HASH(0x183efe8)Entry=HASH(0x1835288) etc.

I have lots of sentences to process (up to 500'000), so that 's why I prefer to avoid to many loops and extract the duplicates in one pass.

So to summarize, I'd need to identify the duplicates and set the flag 'duplicate' to the number of times the duplicate sentence appears.

Before doing the object-oriented code, the approach was simple. But here it is getting more complicated.

I thank you for your help.

Larry


In reply to objects and duplicates by lkperl

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.