aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-05-31 17:20:17 +0100
committerGitHub <noreply@github.com>2020-05-31 17:20:17 +0100
commitd18d2be10d4468055edb0da3e7c15980d3305ac5 (patch)
tree9bfc9af9360082a2549ac55ce99f9257fffc9b40
parent4322b2ed7c473df1e89df1a5f6893a1719436301 (diff)
parent5de3d843c692590a666198b308a46e8af7fc0830 (diff)
downloadperlweeklychallenge-club-d18d2be10d4468055edb0da3e7c15980d3305ac5.tar.gz
perlweeklychallenge-club-d18d2be10d4468055edb0da3e7c15980d3305ac5.tar.bz2
perlweeklychallenge-club-d18d2be10d4468055edb0da3e7c15980d3305ac5.zip
Merge pull request #1772 from manfredi/challenge-062
Solution for Task #1
-rwxr-xr-xchallenge-062/manfredi/perl/ch-1.pl64
1 files changed, 64 insertions, 0 deletions
diff --git a/challenge-062/manfredi/perl/ch-1.pl b/challenge-062/manfredi/perl/ch-1.pl
new file mode 100755
index 0000000000..666c70bb05
--- /dev/null
+++ b/challenge-062/manfredi/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/env perl
+
+#
+# PERL WEEKLY CHALLENGE - 062
+# TASK #1 Sort Email Addresses:
+# Write a script that takes a list of email addresses (one per line)
+# and sorts them first by the domain part of the email address,
+# and then by the part to the left of the @ (known as the mailbox).
+# Note that the domain is case-insensitive, while the mailbox part is case sensitive
+#
+
+use strict;
+
+sub sort_mail (;+) {
+ my @addrs = @{shift()};
+ my @sort = map { $_->[0] }
+ sort { lc($a->[1][1]) cmp lc($b->[1][1]) or $a->[1][0] cmp $b->[1][0] }
+ map { [ $_ , [ split '@' ] ] }
+ @addrs;
+}
+
+sub uniq_mail (;+) {
+ my @addrs = @{shift()};
+ my %uniq = map { $_ => $_ }
+ map { $_->[0] . '@' . lc ($_->[1]) }
+ map { [ split '@' ] }
+ @addrs;
+ my @uniq = sort keys %uniq;
+}
+
+
+my @addrs = <DATA>;
+chomp(@addrs);
+
+my @sort = sort_mail @addrs;
+my @uniq = uniq_mail @addrs;
+
+my %idx = map { 'k' . $_ => $sort[$_] } 0 .. $#sort;
+my %u2m = ();
+
+for my $email (@uniq) {
+ while (my ($k, $v) = each %idx) {
+ if ($v eq $email ) {
+ $u2m{ $k } = $email;
+ }
+ }
+}
+
+my @uniq_sort = map { $u2m{$_} } sort keys %u2m;
+
+{
+ local $"="\n";
+ print "--- Sort:\n@sort\n\n";
+ print "--- Uniq:\n@uniq\n\n";
+ print "--- Uniq & Sort:\n@uniq_sort\n\n";
+}
+
+
+__DATA__
+name@example.org
+rjt@cpan.org
+Name@example.org
+rjt@CPAN.org
+user@alpha.example.org