?
Solved

Perl sort

Posted on 2014-01-02
6
Medium Priority
?
406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
Six Sigma Control Plans
Suggested Courses

764 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