8 Replies - 6879 Views - Last Post: 29 July 2006 - 09:28 AM

#1 ktsirig   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 14-September 05

Map function?

Posted 26 January 2006 - 09:55 AM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Map function?

#2 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Map function?

Posted 26 January 2006 - 10:17 AM

Well, the Map function performs a user defined action on each element...I assume you are looking to eliminate the duplicate from the array. You'll likely have to traverse the array, and look for duplicates...is the array you have made up of elements encased in the parentheses, or is that several arrays you've specified?

Or if it is always the final element, you could use the pop function to remove it.
Was This Post Helpful? 0
  • +
  • -

#3 Voodoo Doll   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 108
  • Joined: 24-January 06

Re: Map function?

Posted 26 January 2006 - 10:57 AM

Quote

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

Is an array the right data structure? If you don't want duplicates then maybe you were really looking for a hash instead of an array. As with everything in Perl, there are tons of ways to do what you want. I like this one though, because it's usually exactly what I want, and tells me that I probably should have been using a hash in the first place. :D
undef %hold;

# Initialize a hash with the array as keys
@hold{@my_array} = ();
# Grab the keys of the hash, sort if you want
# and assign them back to the array
@my_array = sort keys %hold;


Was This Post Helpful? 0
  • +
  • -

#4 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 253
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Map function?

Posted 26 January 2006 - 11:04 AM

Bam! Good call! :) I can't believe my mind didn't go straight to hash (no pun intended) :)
Was This Post Helpful? 0
  • +
  • -

#5 ktsirig   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 14-September 05

Re: Map function?

Posted 26 January 2006 - 02:04 PM

Thanx guys!
Great help!

PS: Amadeus, thanx also for your answer in my earlier post!
Very kind of you!
Was This Post Helpful? 0
  • +
  • -

#6 ktsirig   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 98
  • Joined: 14-September 05

Re: Map function?

Posted 27 January 2006 - 04:31 AM

Oh, I'm stupid...

What I really have is an array of arrays that i want to make unique.
I give an example:
@AoA=(["1","15"], ["2","5"], ["3","4"], ["3","5"], ["3","8"], ["3","5"], ["4","6"], ["4","5"]);
and I want to remove array element ["3","5"] which is seen twice if you notice.
Is that possible? I am confused with this!
Was This Post Helpful? 0
  • +
  • -

#7 Voodoo Doll   User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 108
  • Joined: 24-January 06

Re: Map function?

Posted 27 January 2006 - 08:49 AM

It's possible, but it can get kind of ugly.
@AoA = grep !@hold{join '', @$_}++, @AoA;


Half the fun of Perl is figuring out how stuff like that works. :D
Was This Post Helpful? 0
  • +
  • -

#8 Suman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 05-May 06

Re: Map function?

Post icon  Posted 05 May 2006 - 05:11 AM

I have a problem in eliminating duplicates in array, i want the order of the eliments to be preserved but in hash you would not get the garantee that order is preserved. how do i over come this!!
Was This Post Helpful? 0
  • +
  • -

#9 dcp   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 29-July 06

Re: Map function?

Posted 29 July 2006 - 09:28 AM

In Python, you can define an iterator to return unique elements in order, then create the list...

>>> s = [(1, 12), (1, 13), (2, 14), (2, 15), (2, 3), (2, 16), (3, 15), (3, 3), (3, 20), (3, 5), (3, 6), (3, 20)]
>>> \
def iunique(iterable):
	"""From: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560"""
	seen = set([])
	for i in iterable:
		if i not in seen:
			seen.add(i)
			yield i
>>> [ x for x in iunique(s) ]
[(1, 12), (1, 13), (2, 14), (2, 15), (2, 3), (2, 16), (3, 15), (3, 3), (3, 20), (3, 5), (3, 6)]
>>> 


View PostSuman, on 5 May, 2006 - 07:11 AM, said:

I have a problem in eliminating duplicates in array, i want the order of the eliments to be preserved but in hash you would not get the garantee that order is preserved. how do i over come this!!

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1