Solved

Perl sort

Posted on 2014-01-02
6
376 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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 …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

707 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

15 Experts available now in Live!

Get 1:1 Help Now