dwallach@soda.berkeley.edu (Dan Wallach) (06/08/91)
Well, this is my first perl program, so you'll have to excuse the verbosity. I'm sure Larry, Randall, or Tom could probably write this in about three lines of modem noise, but it does what I want. :-) Basically, you keep a file in your home directory called .news.favorite which lists, in order, what groups you prefer reading. When 100 new newsgroups suddenly get added one day, run the program, and your favorites stay on top of the newsgroup, and the remainder are sorted first by : or !, then alphabetically. JAPH, Dan Wallach dwallach@soda.berkeley.edu P.S. Kudos to Larry and Randall for the Perl book. I just read it, sat down, and cranked this program straight out. (I call this fixnewsrc -- you can call it whatever you want :-) #!/usr/bin/perl # FixNewsrc V1.0 by Dan Wallach # dwallach@soda.berkeley.edu sub counter { $counter++; print STDERR "$counter..." if ($counter % 100) == 0; } sub tally_counter { print STDERR "$counter\n"; } sub clear_counter { $counter = 0; } sub print_favorites { print STDERR "Parsing favorites: "; foreach(<FAVORITE>) { chop; &counter; push (@output, "$newsrc{$_}\n"); undef $newsrc{$_}; } &tally_counter; } if(@ARGV) { print <<NO_MORE_HELP; fixnewsrc, V1.0 by Dan Wallach <dwallach@soda.berkeley.edu> Usage: $0 [no arguments] This program sorts your .newsrc, putting groups you read on top. In addition, if you have a file in your home directory called .news.favorite, then the list of newsgroups in this file appear at the top of your .newsrc, so you can still read groups in your favorite order. Example: rec.humor.funny alt.fan.warlord comp.windows.x.announce ucb.computing.announce comp.lang.perl Here, you will read rec.humor.funny first, and so on through comp.lang.perl, then you will continue reading active groups in alphabetical order. NO_MORE_HELP exit 0; } die "No .newsrc file!" unless -e "$ENV{HOME}/.newsrc"; open(NEWSRC, "$ENV{HOME}/.newsrc") || die "Can't open .newsrc"; # we want to keep this associative array around for printing favorites # so if we've already printed something, we just delete it from the # associative array, and go on. print STDERR "Reading groups: "; &clear_counter; foreach(<NEWSRC>) { chop; &counter; local($group) = split(/[:!]/); # not necessary, but fun $newsrc{$group} = $_; } &tally_counter; # output time... clear the counter and let's deal with the favorites file &clear_counter; if (open(FAVORITE, "$ENV{HOME}/.news.favorite")) { &print_favorites; } else { print "No .news.favorite file found. Just sorting .newsrc\n"; } print STDERR "Sorting..."; @newsrc = sort %newsrc; print STDERR "Generating output: "; # normally, when we go from the associative array to the scalar array, # we get a ton of junk -- the associative array's keys and also, for some # strange reason, blank lines where I undefined stuff earlier. The if/elsif # here weeds all that crap out. foreach(@newsrc) { if(/:/) { &counter; push (@output, "$_\n"); } elsif (/!/) { &counter; push (@output2, "$_\n"); } } &tally_counter; close(NEWSRC); rename("$ENV{HOME}/.newsrc", "$ENV{HOME}/.newsrc.bak") || die "Can't rename .newsrc"; open(NEWSRC, "> $ENV{HOME}/.newsrc") || die "Can't open .newsrc for writing"; print NEWSRC @output, @output2;