The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with arranging data file yahyaaa Shell Programming and Scripting 7 05-29-2008 01:59 PM
re-arranging text in a file with AWK th3g0bl1n UNIX for Dummies Questions & Answers 1 02-12-2008 09:54 AM
create array holding characters from sring then echo array. rorey_breaker Shell Programming and Scripting 5 09-28-2007 05:42 AM
RH8.0 firewall WILL NOT turn off kymberm Linux 3 07-12-2004 06:18 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-24-2008
Registered User
 

Join Date: Jan 2008
Posts: 2
Arranging an array so that duplicates will turn up first

Hi All,

I have an array that contains duplicates as well unique numbers.
ex- (21, 33, 35, 21, 33, 70, 33, 35, 50)

I need to arrange it in such a way that all the duplicates will come up first followed by unique numbers.

Result for the given example should be:
(21, 21, 33, 33, 35, 35, 70, 50)

This a trivial problem in C, creating a binary search tree from the array solves the problem.

As I am new to Perl I don't know indepth of the language.
Would appreciate any help on this regard.

~Ashim
Reply With Quote
Forum Sponsor
  #2  
Old 01-24-2008
Yogesh Sawant's Avatar
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 798
why bother with constructing binary search tree yourself when Perl provides an easy solution:
Code:
#!/usr/bin/perl
# dup_array.pl
my @nums = (21, 33, 35, 21, 33, 70, 33, 35, 50);
print "before sorting\n";
for $i (@nums) {
    print $i, "\n";
}

@nums = sort {$a <=> $b} @nums;

print "after sorting\n";
for $i (@nums) {
    print $i, "\n";
}
Reply With Quote
  #3  
Old 01-24-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
I _think_ the OP wants:

Code:
21, 21, 33, 33, 35, 35, 70, 50
not:

Code:
21, 21, 33, 33, 35, 35, 50, 70
Reply With Quote
  #4  
Old 01-24-2008
Registered User
 

Join Date: Jan 2008
Posts: 2
Yes, what I really want is all the duplicate members followed by unique numbers without any relational order consideration.

So the Sorting might give a correct solution for this example.

But it is not a generic one.It may happen that a duplicate number is present that is greater than an unique number
Reply With Quote
  #5  
Old 01-24-2008
Yogesh Sawant's Avatar
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 798
if you are not a newbie to Perl, this will guide you to achieve what you want
edit: i was being paranoid here.

this may be more appropriate

Last edited by Yogesh Sawant; 01-24-2008 at 08:18 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
perl sort

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0