Solved

Perl sort

Posted on 2014-01-02
6
381 Views
Last Modified: 2014-01-05
Hi ,

I have a list of domain names which need to be sorted based on the topdomain name ( .com,.co.uk,.org etc)

So in the below it was sorted in the format - cc,co.uk,com,net,org

Input:
google.com
yahoo.com
something.net
pbs.org
enduranceinternational.com
google.co.uk
www.pbs.org
store.pbs.org
nicoleg.cc

------------------------------------------------------
output:
nicoleg.cc
google.co.uk
enduranceinternational.com
google.com
yahoo.com
something.net
pbs.org
store.pbs.org
www.pbs.org

Thanks,
Magento
0
Comment
Question by:magento
  • 3
  • 3
6 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39751561
How do you want to specify or determine what the top domains are?  It's not possible to do this without some way of knowing what the "top domains" are (for instance, I would have said uk was the top domain (not co.uk).
0
 
LVL 5

Author Comment

by:magento
ID: 39751628
Hi,

Yes, i understand.

But in the example i state co.uk is the top domain and not uk.

Is it possible to code using the i/p and o/p?

Thanks
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39751653
Are the top domains in your example the only ones we need to worry about?  If not, how do we know when you want the last component or the last two (or more) components of the domain name?

What do you mean by i/p and o/p?  I'm not familiar with those terms.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Author Comment

by:magento
ID: 39751771
I am sorry .

i/o and o/p means input and output respectively.

This is just for my learning so only the given examples is enough to work with .

Thanks
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 39752165
This should do what you want...
use strict;
use warnings;
my %spec = ( uk=>1 );
my $fil = shift or die "Usage: $0 input_file\n";
open IN, $fil or die "could not open $fil: $!";
my @domains = map { chomp; $_ } <IN>;
close IN;
print join("\n", sort custom @domains), "\n";

sub custom {
    # extract the pieces
    my @pa = reverse split /\./, $a;
    my $sa = exists($spec{$pa[0]}) ? "$pa[1].$pa[0]" : $pa[0];
    my @pb = reverse split /\./, $b;
    my $sb = exists($spec{$pb[0]}) ? "$pb[1].$pb[0]" : $pb[0];
    # sort on top domain if different
    return $sa cmp $sb unless ($sa eq $sb);
    # otherwise just sort on the domain string
    # could be extended to sort by domain piece instead of string
    return $a cmp $b;
}

Open in new window

0
 
LVL 5

Author Closing Comment

by:magento
ID: 39758152
Very helpful , thank you .
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now