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?
Map function?
Page 1 of 18 Replies - 6879 Views - Last Post: 29 July 2006 - 09:28 AM
Replies To: Map function?
#2
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.
Or if it is always the final element, you could use the pop function to remove it.
#3
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.
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;
#4
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)
#5
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!
Great help!
PS: Amadeus, thanx also for your answer in my earlier post!
Very kind of you!
#6
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!
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!
#7
Re: Map function?
Posted 27 January 2006 - 08:49 AM
It's possible, but it can get kind of ugly.
Half the fun of Perl is figuring out how stuff like that works.
@AoA = grep !@hold{join '', @$_}++, @AoA;
Half the fun of Perl is figuring out how stuff like that works.
#8
Re: Map function?
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!!
#9
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)] >>>
Suman, 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!!
Page 1 of 1

New Topic/Question
Reply


MultiQuote




|