Re: deleting duplicates in array using references



On Jul 30, 5:18 pm, billb <billbea...@xxxxxxx> wrote:
On 30 Jul, 19:46, Paul Lalli <mri...@xxxxxxxxx> wrote:





On Jul 30, 2:37 pm, billb <billbea...@xxxxxxx> wrote:

i have a multidimensional array, but i want to delete duplicate
entries based on the first element of each 'row'.

my @array = ( [UK9004411, A140, B, 0.040] , [UK0030239, H7140, H,
0.030] , [UK0030239, S1393, M1, 0.030] , [UK0012821, H4030, H,
0.010] , [UK0012821, H4060, H, 0.010] );

and I want to end up with
( [UK9004411, A140, B, 0.040] , [UK0030239, H7140, H, 0.030] ,
[UK0012821, H4030, H, 0.010] )

my %seen;
my @nodups = grep { !$seen{$_->[0]}++ } @array;

ah, very simple and very fast as well! I'll have to understand how
this is working. It uses a hash I see.

It helps if you expand it out to remove all the "shortcuts"

my %seen;
my @nodups;
foreach my $elem (@array) {
if (! $seen{$elem->[0]}) {
push @nodups, $elem;
}
$seen{$elem->[0]}++;
}

So we're looping through the 2d array, and we check to see if the
first element of the current array reference has been "seen" yet. If
not, we add this array reference to our list of no duplicates. Then
we increment the number of times we've "seen" this element, so that if
the same element is seen again, we won't add it next time.

The shortcuts:
* a foreach-if-push combination is equivalent to grep(). grep selects
only those elements from a list for which the if condition holds.
* in the grep, $_ is used to represent the current element of the
array (rather than $elem as in the above expansion)
* The ++ operator is applied to the same expression as when we're
checking the current value of $seen{$_->[0]}, because a post-fix ++
increments the value *after* returning that value. That is:
$x = $foo++;
is equivalent to:
$x = $foo;
$foo++;

In contrast,
$x = ++$foo;
is equivalent to
$foo++;
$x = $foo;

Many thanks.

You're welcome

Paul Lalli

.



Relevant Pages

  • Re: Find a line in a text file then print a field
    ... Here is "grep" part of my perl script so far (ommitting the various ... the matching lines in an array. ...
    (perl.beginners)
  • Re: How to sort an array which contains a value several times ?
    ... Jay Savage a e'crit: ... > because we want to iterate over @array and filter duplicates out (you ... > cases $is_new remains 0 and grep filters that element out. ...
    (perl.beginners)
  • Re: Re: How to sort an array which contains a value several times ?
    ... > because we want to iterate over @array and filter duplicates out (you ... > cases $is_new remains 0 and grep filters that element out. ...
    (perl.beginners)
  • Re: problem about array of object
    ... You're assigning a pointer-to-elem ... I try different constructors, copy, defaultt, and my own constructor, they ... >> I have an object "elem", there are only simple functions inside, like ... >> Now I have another class "Base", need an array of elem to initialize ...
    (comp.lang.cpp)
  • Re: Can I Grep multiple lines??
    ... grep does not change array elements. ... That distinction gets blurry when the test function in a filter has ... from the original array, but the corresponding elements keep the SAME ...
    (comp.lang.perl.misc)