[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Perl sort

Posted on 2014-01-02
6
Medium Priority
?
411 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 27

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 27

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

 
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 27

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

649 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