in reply to Find duplicate elements from an array

You're most likely using the wrong data structure. Better try a hash of arrays...

Cheers Rolf

  • Comment on Re: Find duplicate elements from an array

Replies are listed 'Best First'.
Re^2: Find duplicate elements from an array
by mjscott2702 (Pilgrim) on Nov 22, 2010 at 08:52 UTC
    Yep, HoA is what you need, keyed by name, simply unshift (if you want the second page number before the first - you specify this, but the example doesn't back it up) the page number onto the beginning of the array:

    use strict; use warnings; use Data::Dumper; my %HoA = (); while (<DATA>) { if (/(.+)\s+(\d+)\s*$/) { unshift(@{$HoA{$1}}, $2); } } print Dumper(\%HoA); __DATA__ Barmparas, Galinos, 1120 Barmparas, Galinos, 1410 Beal, Alan, 866 Beall, Dawson, 1062

    Output:

    $VAR1 = { 'Beal, Alan,' => [ '866' ], 'Beall, Dawson,' => [ '1062' ], 'Barmparas, Galinos,' => [ '1410', '1120' ] };
      Thank you so much.. mjscott2702