Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Get only unique values into array

by Anonymous Monk
on Jan 28, 2015 at 18:59 UTC ( [id://1114799]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Monks!
I am trying to check if this particular value already is in this array to avoid having duplicated values in it.
I just can't get this code to work.
I have this code sample that I hope shows what and where I am having issues.

This is the first array:
… my @loop_names = (); foreach my $row (@{$names_data}) { my %row_data; $row_data{NAMES} = $row->{NAMES} || ''; push(@loop_names, \%row_data); } # Dumper of @loop_names; =code $VAR1 = { 'NAMES' => 'John Doe' }; $VAR2 = { 'NAMES' => 'Mary Lou' } =cut

This is from the second array:
my @loop_addr = (); foreach my $row (@{$all_party_id_data}) { my %row_data; $row_data{FULL_NAME} = $row->{FULL_NAME} || ''; $row_data{ADDR} = $row->{ADDR} || ''; $row_data{ZIP} = $row->{ZIP} || ''; warn Dumper $row_data{FULL_NAME}; # it will get "Mary Lou" # I just want @loop_names to have what is unique @loop_names = map /$row_data{FULL_NAME}/ ? () : $_, @loop_names; $row_data{N_DATA} = \@loop_names; push(@loop_addr, \%row_data); } # Dumper of @loop_addr =code $VAR1 = { 'FULL_NAME' => 'Mary Lou', 'ADDR' => '100 - Main Street', 'ZIP' => '12345', 'EMAIL_DATA' => [ { 'NAMES' => 'John Doe' }, { 'NAMES' => 'Mary Lou' # this shouldn't be here + } ], }; =cut


I would like this array to only have this at the end, because “John Doe” is the only unique value:
$VAR1 = { 'FULL_NAME' => 'Mary Lou', 'ADDR' => '100 - Main Street', 'ZIP' => '12345', 'EMAIL_DATA' => [ { 'NAMES' => 'John Doe' }, ], };

Thanks for looking!

Replies are listed 'Best First'.
Re: Get only unique values into array
by pme (Monsignor) on Jan 28, 2015 at 19:53 UTC
Re: Get only unique values into array
by stonecolddevin (Parson) on Jan 28, 2015 at 23:23 UTC

    Take a look at Set::Scalar.

    Three thousand years of beautiful tradition, from Moses to Sandy Koufax, you're god damn right I'm living in the fucking past

Re: Get only unique values into array
by Anonymous Monk on Jan 28, 2015 at 19:59 UTC
    @loop_names = map /$row_data{FULL_NAME}/ ? () : $_, @loop_names;
    @loop_names doesn't contain strings, it has hashrefs, which stringify to something useless. So that's not going to work. Also, grep is used for filtering elements of a list.
Re: Get only unique values into array
by Laurent_R (Canon) on Jan 28, 2015 at 19:49 UTC
    Whenever you think about unique value, think hash. In other terms, use a hash to check whether the value has already been seen.

    Je suis Charlie.
      Hum, it looks like what you are asking is more complicated than what I originally understood. And I am not sure I do understand what your are asking...

      Je suis Charlie.
Re: Get only unique values into array
by poj (Abbot) on Jan 28, 2015 at 19:53 UTC
    # I just want @loop_names to have what is unique # add --------------- @loop_names = map { $_->{'NAMES'}=~/$row_data{FULL_NAME}/ ? () : $_ } @loop_names;

    Are you sure you want to overwrite @loop_names ?

    poj
      Yes, as long as it will only have unique values in there. I don't see any other way.
Re: Get only unique values into array
by BillKSmith (Monsignor) on Jan 28, 2015 at 23:55 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1114799]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 09:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found