PERL   35

# navigalb pl

Guest on 17th May 2022 01:22:51 AM

1. sub bynumber { \$a <=> \$b; }
2.
3. open(Artist,"<artist.pdb")||die "Can't open artist.pdb!\n";
4. open(Album,"<album.pdb")||die "Can't open album.pdb!\n";
5. open(Song,"<song.pdb")||die "Can't open song.pdb!\n";
6. open(On,"<on.pdb")||die "Can't open on.pdb!\n";
7. open(Sings,"<sings.pdb")||die "Can't open sings.pdb!\n";
8. open(SeeAlso, "<seealso.pdb")||die "Can't open sings.pdb!\n";
9.
10. \$i=0;
11. \$maxartno = 0;
12. while(<Artist>){
13.   (\$artno,\$artist,\$abcartist) = split(/\|/);
14.   chop \$abcartist;
15.   \$artist{\$artno} = \$artist;
16.   \$abcartist{\$artno} = \$abcartist;
17.   \$maxartno = \$artno if (\$artno > \$maxartno);
18.   \$i++;
19. }
20. print "Artist: \$i records\n";
21. \$i=0;
22. while(<SeeAlso>){
23.   (\$one,\$two) = split(/\|/);
24.   chop \$two;
25.   @temp = split(/\|/,\$seealso{\$one});
26.   \$seealso{\$one} = join('|',@temp,\$two);
27.   @temp = split(/\|/,\$seealso{\$two});
28.   \$seealso{\$two} = join('|',@temp,\$one);
29.   \$i++;
30. }
31. print "SeeAlso: \$i records\n";
32. \$i = 0;
33. while(<Album>){
34.   (\$albno,\$artno,\$album,@details) = split(/\|/);
35.   \$artno{\$albno} = \$artno;
36.   \$album{\$albno} = \$album;
37.   \$i++;
38. }
39. print "Album: \$i records\n";
40. \$i = 0;
41. \$maxsongno = 0;
42. while(<Song>){
43.   (\$songno,\$song)=split(/\|/);
44.   chop \$song;
45.   \$song{\$songno} = \$song;
46. #  \$song =~ s/&/and/;
47. #  \$song =~ tr/A-Z/a-z/;
48. #  \$song =~ tr/a-z0-9 //cd;
49. #  @temp = split(/\|/,\$songno{\$song});
50. #  push(@temp, \$songno);
51. #  \$songno{\$song} = join('|',@temp);
52.   \$maxsongno = \$songno if (\$songno > \$maxsongno);
53.   \$i++;
54. }
55. print "Song: \$i records\n";
56. \$i=0;
57. while(<On>){
58.   (\$onalb,\$onsong,\$odetails) = split(/\|/);
59.   chop \$odetails;
60.   @temp = split(/\|/,\$onalb{\$onsong});
61.   \$onalb{\$onsong} = join('|',@temp,\$onalb);
62.   \$odetails{"\$onalb|\$onsong"} = \$odetails;
63.   \$i++;
64. }
65. print "On: \$i records\n";
66. \$i = 0;
67. while(<Sings>){
68.   (\$sartno,\$ssongno) = split (/\|/);
69.   chop \$ssongno;
70.   @temp = split(/\|/,\$sartno{\$ssongno});
71.   \$sartno{\$ssongno} = join('|',@temp,\$sartno);
72.   \$i++;
73. }
74. print "Sings: \$i records\n";
75. close Artist;
76. close Album;
77. close Song;
78. close On;
79. close Sings;
80. close SeeAlso;
81. print "aLbum aRTist Song Quit\n";
82. while(1){       # do this forever
83.     print "Enter navigation code, or H for help:";
84.     \$input = <STDIN>; chop \$input;
85.     \$call = substr(\$input, 0, 1);
86.     CHOICE: {
87.         if ("L" eq \$call || "l" eq \$call){
88.           &album(substr(\$input,1));
89.           last CHOICE;
90.         }
91.         if ("R" eq \$call || "r" eq \$call){
92.           &artist(substr(\$input,1), 'r');
93.           last CHOICE;
94.         }
95.         if ("T" eq \$call || "t" eq \$call){
96.           &artist(substr(\$input,1), 't');
97.           last CHOICE;
98.         }
99.         if ("S" eq \$call || "s" eq \$call){
100.           &song(substr(\$input,1));
101.           last CHOICE;
102.         }
103.         if ('*' eq \$call){
104.           &pick(substr(\$input,1,1));
105.           last CHOICE;
106.         }
107.         if ("H" eq \$call || "h" eq \$call){
108.           &help;
109.           last CHOICE;
110.         }
111.         if ("Q" eq \$call || "q" eq \$call){
112.           exit 0;
113.         }
114.         print "Type H for help.\n"
115.     } #CHOICE
116. }
117.
118. sub artist{
119.   local(\$argument, \$searchtype) = @_;
120.   if (\$argument !~ /^\d*\$/) {   # not a number
121.     \$temp = -1;
122.     foreach \$artno (sort bynumber (keys %artist)) {
123.       if ((\$artist{\$artno} =~ /^\$argument/i) ||
124.           (\$abcartist{\$artno} =~ /^\$argument/i)) {
125.         print "\u\$searchtype\$artno:\$artist{\$artno}\n";
126.         \$temp = (-1 == \$temp) ? \$artno : -2;
127.       }
128.     }
129.     return 0 if (\$temp < 0);
130.     print "Right?";
131.     return 0 if (<STDIN> !~ /^y/i);
132.     \$artno = \$temp;
133.   }
134.   else {
135.     \$artno = \$argument;
136.   }
137.   if (! defined(\$artist{\$artno})){ # no such artist
138.     print "Invalid artist.\n";
139.     return 1;         #like anyone cares
140.   }
141.   print "\u\$searchtype\$artno:\$artist{\$artno} (\$abcartist{\$artno})\n";
142.   if ('r' eq \$searchtype) {
143.     foreach \$albno (sort bynumber (keys %album)) {
144.       if (\$artno{\$albno} == \$artno) {
145.         print "\tL\$albno:\$album{\$albno}\n";
146.       }
147.     }
148.   }
149.   elsif ('t' eq \$searchtype) {
150.     foreach \$songno (sort bynumber (keys %sartno)) {
151.       if (\$sartno{\$songno} =~ /^(\d*\|)*\$artno(\|\d*)*\$/) {
152.         print "\tS\$songno:\$song{\$songno}\n";
153.       }
154.     }
155.   }
156.   if (defined(\$seealso{\$artno})) {
158.     foreach \$seealso (sort bynumber (split(/\|/,\$seealso{\$artno}))) {
159.       print "\t\u\$searchtype\$seealso:\$artist{\$seealso}\n";
160.     }
161.   }
162. }
163.
164. sub album{
165.   local(\$argument) = @_;
166.   if (\$argument !~ /^\d*\$/) {   # not a number
167.     \$temp = -1;
168.     foreach \$albno (sort bynumber (keys %album)) {
169.       if (\$album{\$albno} =~ /^\$argument/i) {
170.         print "L\$albno:\$album{\$albno} by \$artist{\$artno{\$albno}}\n";
171.         \$temp = (-1 == \$temp) ? \$albno : -2;
172.       }
173.     }
174.     return 0 if (\$temp < 0);
175.     print "Right?";
176.     return 0 if (<STDIN> !~ /^y/i);
177.     \$albno = \$temp;
178.   }
179.   else {
180.     \$albno = \$argument;
181.   }
182.   if (! defined(\$album{\$albno})){ # no such album
183.     print "Invalid album.\n";
184.     return 1;         #like anyone cares
185.   }
186.   print "L\$albno:\$album{\$albno}\n";
187.   print "\t\tby R\$artno{\$albno}:\$artist{\$artno{\$albno}}\n";
188.   foreach \$onrec (sort bynumber (keys %odetails)) {
189.     if (\$onrec =~ /^\$albno\|/) {
190.       (\$nothing, \$songno) = split (/\|/, \$onrec);
191.       print "\tS\$songno:\$song{\$songno}\n";
192.     }
193.   }
194. }
195.
196. sub song{
197.   local(\$argument) = @_;
198.   if (\$argument !~ /^\d*\$/) {   # not a number
199.     \$temp = -1;
200.     foreach \$songno (sort bynumber (keys %song)) {
201.       if (\$song{\$songno} =~ /^\$argument/i) {
202.         print "S\$songno:\$song{\$songno}\n";
203.         \$temp = (-1 == \$temp) ? \$songno : -2;
204.       }
205.     }
206.     return 0 if (\$temp < 0);
207.     print "Right?";
208.     return 0 if (<STDIN> !~ /^y/i);
209.     \$songno = \$temp;
210.   }
211.   else {
212.     \$songno = \$argument;
213.   }
214.   if (! defined(\$song{\$songno})){ # no such album
215.     print "Invalid song.\n";
216.     return 1;         #like anyone cares
217.   }
218.   print "S\$songno:\$song{\$songno}\n";
219.   @sartno = split (/\|/, \$sartno{\$songno});
220.   \$temp = shift @sartno;
221.   print " by\tR\$temp:\$artist{\$temp} \n";
222.   while (\$sartno = shift @sartno){
223.     print "  and\tR\$sartno:\$artist{\$sartno}\n";
224.   }
225.   @onalb = split (/\|/, \$onalb{\$songno});
226.   \$temp = shift @onalb;
227.   print " on\tL\$temp:\$album{\$temp}\n";
228.   while (\$onalb = shift @onalb){
229.     print "  and\tL\$onalb:\$album{\$onalb}\n";
230.   }
231. }
232.
233. sub pick {
234.   local(\$picktype) = @_;
235.   if (("\u\$picktype" eq 'R')||("\u\$picktype" eq 'T')){
236.     @temp = keys %artist;
237.     \$arg = \$temp[int(rand(scalar(@temp)))];
238.     &artist(\$arg, \$picktype);
239.   }
240.   elsif ("\u\$picktype" eq 'L'){
241.     @temp = keys %album;
242.     \$arg = \$temp[int(rand(scalar(@temp)))];
243.     &album(\$arg);
244.   }
245.   elsif ("\u\$picktype" eq 'S'){
246.     @temp = keys %song;
247.     \$arg = \$temp[int(rand(scalar(@temp)))];
248.     &song(\$arg);
249.   }
250. }
251.
252. sub help {
253.   print <<'EndOfHelp';
254. This is a database of the artists, albums, and songs in Denis's vinyl
255.   collection. You can query any of those three lists.  The first character
256.   of your input specifies which list is queried and what info is returned by
257.   a successful lookup:
258.     R: Artists: List of albums
259.     T: Artists: List of songs
260.     L: Albums: Artist, list of songs
261.     S: Songs: Artist(s), Album(s)
262.   If the remainder is a number, that [album,artist,song] number is looked up.
263.     Otherwise, the remainder is checked, non-case-sensitively, against
264.     everything in the list.  It assumes your input is the beginning of the
265.     name, but you can get around that if you're clever and know Perl regular
266.     expression syntax.  (It uses the call /^\$arguments/i if that helps you
267.     any.)  First names and last names will both work fine for solo or
268.     pseudosolo artists.
269.   Also, you can get a random artist, album, or song by entering something
270.     like "*S", where the s can be any of the four lookup codes.
271. This has been crashing my 486 lately, so you may want to save things before
272.   running it on a personal computer.  I doubt it will break a box running a
273.   real operating system.  But, Caveat Ludator.
274. EndOfHelp
275. }