Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 26th, 2006, 10:00 AM   #1
ktsirig
Programmer
 
Join Date: Oct 2005
Posts: 54
Rep Power: 4 ktsirig is on a distinguished road
Unique array?

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?
ktsirig is offline   Reply With Quote
Old Jan 26th, 2006, 11:22 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
This may get you going...

my %hash = map { $_ => 1 } @array;
my @array2 = sort keys %hash;

Where array2 is your unique array...
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jan 26th, 2006, 3:27 PM   #3
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 3 Kaja Fumei is on a distinguished road
A page from the Perl Cookbook:
http://www.unix.org.ua/orelly/perl/cookbook/ch04_07.htm
Kaja Fumei is offline   Reply With Quote
Old Jan 27th, 2006, 4:27 AM   #4
ktsirig
Programmer
 
Join Date: Oct 2005
Posts: 54
Rep Power: 4 ktsirig is on a distinguished road
array of arrays [correct topic]

Hi guys,
thanx for your time...
I seem to have made a mistake here...
What I have is an array of arrays that i want to make unique. That is, I have:
@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!
ktsirig is offline   Reply With Quote
Old Mar 31st, 2006, 5:53 PM   #5
LudakoT
Newbie
 
Join Date: Mar 2006
Location: Macedonia
Posts: 12
Rep Power: 0 LudakoT is on a distinguished road
grep and one hash would do just fine
@array = grep {!$hash{$_}++} @array;
where %hash should be 'defined' as an empty hash () and @array is your array where you have duplicate elements.
LudakoT is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:59 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC