![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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";
}
|
|
#3
|
||||
|
||||
|
I _think_ the OP wants:
Code:
21, 21, 33, 33, 35, 35, 70, 50 Code:
21, 21, 33, 33, 35, 35, 50, 70 |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
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. |
||||
| Google The UNIX and Linux Forums |