'Map' function Perl

They have: 53 posts

Joined: Oct 2005

Hi everybody!
I have a question concering arrays..
Say I have the following array:
(1,12)
(1,13)
(2,14)
(2,15)
(2,3)
(2,16)
(3,15)
(3,3)
(3,20)
(3,5)
(3,6)
(3,20)

Everything is OK except that pair (3,20) is repeated twice. How can I avoid it? 'Map' function is the answer?

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Map is used to run parameters through a given expression (could be a function, for example). You could probably write a solution to test for duplicates yourself using map, but that would be reinventing the wheel.

I gather you are trying to make what's called a 'set', which is a list containing no duplicate entries. You can use Ron Savage's Set::Array module to do things with your arrays to make sets or identify duplicates or whatever you need specifically.

Smiling

They have: 7 posts

Joined: Jan 2006

to check if an element is in the array already, check it using in_array first, only insert if it returns false (http://us2.php.net/in_array). Also, if you want to get rid of duplicate entries, you can run array_unique to return only one of each element in the array. (http://us2.php.net/array_unique)

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

If you can, post the code you're working with. That would help us help you better.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.