Solved

Perl sort

Posted on 2014-01-02
6
390 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

839 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