home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2652 < prev    next >
Internet Message Format  |  1991-02-02  |  4KB

  1. From: composer@chem.bu.edu (Jeff Kellem)
  2. Newsgroups: alt.sources,comp.misc,alt.folklore.computers
  3. Subject: Re: Acronym lookup tool
  4. Message-ID: <COMPOSER.91Feb2145029@chem.bu.edu>
  5. Date: 2 Feb 91 19:50:29 GMT
  6.  
  7. Dave,
  8.  
  9. Thanks for posting the acronym database stuff.  For those that have
  10. Perl, below is a quick rewrite of Dave's acronym lookup tool in Perl.
  11. It's not fancy, just a quick translation to Perl.  Same logic, etc..
  12.  
  13. The perl script is also it's own man page (thanks to Larry Wall for the
  14. nroff/perl trickery).  The only thing I added was to look for the
  15. acronyms database in a few default locations, along with having the ACRON
  16. environment variable override the defaults.  The Perl code could look
  17. better, but hey.. it's just a quick translation.  ;-)
  18.  
  19. Enjoy...
  20.  
  21.             -jeff
  22.  
  23. Jeff Kellem
  24. Internet: composer@chem.bu.edu
  25.  
  26. ===CUT HERE===whats (in Perl)===
  27. #!/usr/local/bin/perl
  28. 'di';
  29. 'ig00';
  30. #
  31. # file: whats (or whats.pl or whats.perl)
  32. #
  33. # usage: whats [ acronym [defn...]]
  34. # See man page after __END__ line
  35. #
  36.  
  37. $ACRON = $ENV{'ACRON'}
  38.     || (-f '/usr/local/lib/acronyms' && '/usr/local/lib/acronyms')
  39.     || (-f './acronyms' && './acronyms')
  40.     || (-f './acron' && './acron');
  41.  
  42. ($me = $0) =~ s|.*/||;
  43. die "$me: Can't find acronym file\n" unless $ACRON;
  44.  
  45. $lookup = shift || &input('What is what? ');
  46. $lookup =~ tr/a-z/A-Z/;
  47.  
  48. $def = join(" ", @ARGV);    # gather up rest of args
  49.  
  50. open ACRON || die "$me: Can't open acronym file: $!\n";
  51. while (<ACRON>) {
  52.     next unless /^$lookup\s*-/o;
  53.     push(@found, $_);    # or could do $found .= $_; $count++;
  54. }
  55. close ACRON;
  56.  
  57. if (!$def && @found) {
  58.     print @found;
  59.     exit;
  60. } elsif (@found) {
  61.     # or    $#found+1
  62.     print scalar(@found), " occurrences found.\n", @found;
  63.     if (&input("Still want to add $lookup? ") !~ /^y$/oi) {
  64.     exit;
  65.     }
  66. }
  67.  
  68. $def = $def || &input("What does $lookup stand for? ") || exit;
  69. $lookup = sprintf("%-8s- $def", $lookup);
  70.  
  71. open(ACRON, ">> $ACRON") || die "$me: Can't append to $ACRON: $!\n";
  72. print ACRON "$lookup\n";
  73. close ACRON;
  74. print "$lookup\n";
  75.  
  76. sub input {
  77.     print @_[0];
  78.     chop($_ = <STDIN>);
  79.     $_;
  80. }
  81. ###############################################################
  82.  
  83.     # These next few lines are legal in both Perl and nroff.
  84.  
  85. .00;                       # finish .ig
  86.  
  87. 'di           \" finish diversion--previous line must be blank
  88. .nr nl 0-1    \" fake up transition to first page again
  89. .nr % 0         \" start at page 1
  90. '; __END__ ##### From here on it's a standard manual page #####
  91.  
  92. .TH WHATS 1 "February 2, 1991"
  93. .AT 3
  94. .SH NAME
  95. whats \- lookup acronym/abbreviation in database
  96. .SH SYNOPSIS
  97. .B whats [acronym [defn...]]
  98. .SH DESCRIPTION
  99. .IR Whats ,
  100. without any arguments, prompts for an acronym to look up.
  101.  
  102. With one argument,
  103. .I whats
  104. prints all entries from the database
  105. that match the argument.  If no matches are found,
  106. .I whats
  107. prompts for expansion of the acronym or abbreviation.
  108.  
  109. With more than one argument,
  110. .I whats
  111. scans the database for
  112. entries matching the first argument.  If any matches are found,
  113. .I whats
  114. prints the matching entries and asks if the remaining
  115. arguments should be added as a new entry.  If no matches are
  116. found, a new entry is created for the first argument with the
  117. remaining arguments forming the expansion.
  118.  
  119. .SH ENVIRONMENT
  120. .IP ACRON
  121. The location of the acronym database.  Overrides any default locations.
  122. .SH FILES
  123. The acronym database is searched for in the following order:
  124.     $ACRON
  125.     /usr/local/lib/acronyms
  126.     ./acronyms
  127.     ./acron
  128. .SH AUTHOR
  129. Original shell script written by:
  130. .br
  131. Dave Sill (dsill@nswc-oas.arpa) (de5@ornl.gov as of 6/90)
  132. Naval Surface Warfare Center (NSWC), Dahlgren, VA
  133. .PP
  134. Perl translation written by:
  135. .br
  136. Jeff Kellem (composer@chem.bu.edu), 2 Feb 1991
  137. .SH "SEE ALSO"
  138.  
  139. .SH BUGS
  140. Not blindingly fast.  The Perl version should work on any system that
  141. runs Perl.
  142. .PP
  143. The Perl version is basically just a straight translation of the original
  144. shell script.
  145. .PP
  146. The Perl version should probably be reorganized a tiny bit.
  147.