|
[SPOILER] Perl 'Easy' Quiz of the Week #2005-1: msg#00049lang.perl.qotw.discuss
Well, umm..this is my first post to this list, so hopefully I don't break anything. My solution wasn't too fantastic, but it works. I load every prefix into a hash key, and the value of each key is an array of suffixes. #!/usr/bin/perl -w use strict; my ($in, $out) = @ARGV; my %prefixes; open(IN, '<', $in) || die $!; open(OUT, '>', $out) || die $!; while(<IN>) { chomp(my $line = $_); if($line =~ /^\w+\.[A-Z]$/) { my ($prefix, $suffix) = split(/\./, $line); $prefixes{$prefix} = () if(!$prefixes{$prefix}); push @{$prefixes{$prefix}}, $suffix; } } for my $pref (sort keys %prefixes) { push @{$prefixes{$pref}}, 'M' if(!search('M', @{$prefixes{$pref}})); print OUT map{"$pref.$_\n";}sort @{$prefixes{$pref}} } close(IN); close(OUT); sub search { my ($f, @arr) = @_; map{return 1 if /^$f$/}@arr; return 0; } |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: Perl 'Easy' Quiz of the Week #2005-1: 00049, Ronald J Kimball |
|---|---|
| Next by Date: | [SPOILER] Minimalistic (but CPAN-enabled) solution: 00049, Shlomi Fish |
| Previous by Thread: | Re: [SPOILER] Perl 'Easy' Quiz of the Week #2005-1i: 00049, Zed Lopez |
| Next by Thread: | Fw: [SPOILER] Perl 'Easy' Quiz of the Week #2005-1: 00049, Bill Smith |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |