-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitemtrack.plx
More file actions
367 lines (330 loc) · 8.26 KB
/
itemtrack.plx
File metadata and controls
367 lines (330 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/perl
# itemtrack.plx
use warnings;
use strict;
#Useful constants
#Item name string
my $ITEMNAME = "Item Name: ";
#Item tags
my $TAG = "Tags: ";
#Description of the item's location
my $DESC = "Description: ";
#SUBROUTINE PROTOTYPES
my @names;
my %nametag;
my %namedesc;
my %tagname;
my $file;
#Indicates if the contents of the text file has been changed
my $changedflag = 0;
#my $newname;
#while (@ARGV) {
# my $option = shift;
# if ($option =~ /--add/) {
# $newname = shift;
# add($newname);
# }
# elsif ($option =~ /--tags/) {
# my $tags = shift;
# setTags($tags, $newname);
# }
# else {
# $file = $option;
$file = shift;
open my $DB, "$file" or die "Unable to open $file";
readin($DB);
close $DB;
#We should add some code to deal with what happens when there is no
#filename given for my $file = shift to work.
# }
#}
my $contflag = 1;
do {
print <<EOF;
(0) Exit
(1) Search for item
(2) Edit item
(3) Print all items, tags, and descriptions
EOF
print "> ";
my $choice = <STDIN>;
print "\n";
if ($choice == 0) {
$contflag = 0;
} elsif ($choice == 1) {
lookup();
} elsif ($choice == 2) {
edit();
} elsif ($choice == 3) {
print_all();
} else {
print "Invalid input. Use only 0, 1, 2, or 3!\n"
}
} while($contflag);
if ($changedflag) {
print "Addition/removal of entries has occurred. Write changes (y/n)? ";
my $stdin = <STDIN>;
chomp $stdin;
writeout() if ($stdin eq "y");
}
#SUBROUTINES
sub readin {
my $fh = shift;
my $name;
my $linenum;
while (<$fh>) {
#Blank lines are separators between items. Also skip comments
next if ($_ eq "\n") || (/^#/);
if (/^$ITEMNAME/) { #name line
$names[$#names + 1] = substr($_, length($ITEMNAME));
chomp $names[$#names];
$name = $names[$#names];
}
elsif (/^$TAG/ && defined $name) { #tag line
$nametag{$name} = substr($_, length($TAG));
chomp $nametag{$name};
#This is an excellent spot to form the hash for tags -> item name.
additem2tag($nametag{$name}, $name);
}
elsif (/^$DESC/ && defined $name) { #description of location line
$namedesc{$name} = substr($_, length($DESC));
chomp $namedesc{$name};
}
else { #Now you have something really weird in this file
print "Something weird going on at $linenum in $file.\n";
}
}
}
sub print_all {
for (@names) {
print "Item Name: $_\n";
print "Tags: $nametag{$_}\n" if (exists $nametag{$_});
print "Description: $namedesc{$_}\n" if (exists $namedesc{$_});
print "\n";
}
}
#additem2tag(<list of tags>, <name of item described by tags>)
sub additem2tag {
my ($taglist, $name) = @_;
#split up the list of tags into individual tokens
#Add each of the tags as keys to the %tagname hash, with the name as the value
for my $tag ($taglist =~ /\w+/g)
{
unless (exists $tagname{$tag}) {
#The [] brackets form a _reference_ to an array.
$tagname{$tag} = [ $name ];
}
else { #tag already exists in the hash
#The following line does not work because the reference array is COPIED
#@namear = @{$tagname{$tag}};
#We can make it a bit simpler to read, however, by doing this,
#which adds $name to the referenced array
my $ref = $tagname{$tag};
${$ref}[$#{$ref} + 1] = $name;
}
}
}
sub lookup {
print "\n";
my $contflag = 1;
do {
print <<EOF;
(0) Back
(1) Search by name
(2) Search by tags
EOF
print "> ";
my $choice = <STDIN>;
print "\n";
if ($choice == 0) {
$contflag = 0;
}
elsif ($choice == 1) { #Search by name
searchbyname();
}
elsif ($choice == 2) { #Search using tags
searchbytags();
}
else {
print "Input only 0, 1, or 2!\n";
}
} while ($contflag);
}
sub edit {
my $contflag = 1;
do {
print <<EOF;
(0) Back
(1) Add
(2) Remove
EOF
print "> ";
my $choice = <STDIN>;
print "\n";
chomp $choice;
if ($choice == 0) {
$contflag = 0;
}
elsif ($choice == 1) { #Add item
print "Name of item to add: ";
my $newname = <STDIN>;
chomp $newname;
print "Tags of the item (separate by space): ";
my $newtags = <STDIN>;
chomp $newtags;
print "Description of item's location: ";
my $newdesc = <STDIN>;
chomp $newdesc;
print "\n";
additem($newname, $newtags, $newdesc);
$changedflag = 1;
}
elsif ($choice == 2) { #Remove item
print "Name of item to remove: ";
my $choice = <STDIN>;
chomp $choice;
removeitem($choice);
$changedflag = 1;
}
} while ($contflag)
}
sub printitem {
my @matches = @_;
for my $i (0 .. $#matches) {#then search key exists!
my $name = $matches[$i];
print "Item Name: $name\n";
print "Tags: $nametag{$name}\n" if exists $nametag{$name};
print "Description: $namedesc{$name}\n" if exists $nametag{$name};
print "\n";
}
}
sub searchbyname {
print "Item name> ";
my $choice = <STDIN>;
print "\n";
chomp $choice;
my @matches = grep /$choice/i, @names;
printitem(@matches);
}
sub searchbytags {
print "Tags to search for> ";
my $choice = <STDIN>;
print "\n";
chomp $choice;
#1) Get all the items from the first tag's hash (@items)
#2) Search through each of the items in @items. The tags of each of the items
# should contain all the remaining tags
#3) Eliminate the ones that don't have all of the remaining tags
my @tags = ($choice =~ /\w+/g);
my $i = 1;
my @intersection; #This will be the results array;
for (@tags) {
if (exists $tagname{$_}) {
@intersection = @{$tagname{$_}};
last;
}
}
while($i < scalar(@tags) && @intersection) { #There are array elements
my $tag = $tags[$i];
#let's form the intersection of the arrays referenced by the tags
if (exists $tagname{$tag}) {
my @namesoftag = @{$tagname{$tag}};
#print "\nFor $tag: @namesoftag\n";
#print "\n@intersection\n";
my %count;
for my $thename (@intersection, @namesoftag) {
$count{$thename}++;
}
my @newintersection;
for my $thename (keys %count) {
if ($count{$thename} > 1) {
push @newintersection, $thename;
}
}
@intersection = @newintersection;
}
$i++;
}
printitem(@intersection);
}
#3 parameters: name, tags, description
sub additem {
my $name = shift;
my $tags = shift;
my $desc = shift;
#check for matches or existing items:
my @matches = grep /^$name$/, @names;
#Replace spaces in tags with commas
$tags =~ s/[^\w\d]+/,/g;
print "@matches\n";
if (@matches) {
removeFromTags($nametag{$matches[0]}, $name);
$nametag{$matches[0]} = $tags;
additem2tag($tags, $name);
$namedesc{$name} = $desc;
}
else { #We can just add it in! We're all clear!
push @names, $name;
$nametag{$name} = $tags;
additem2tag($tags, $name);
$namedesc{$name} = $desc;
}
}
sub removeFromTags {
my $tags = shift;
my $thename = shift;
for my $tag ($tags =~ /\w+/g) {
my $index = indexof($tagname{$tag}, $thename);
unless ($index == -1) {
splice (@{$tagname{$tag}}, $index, 1);
}
}
}
sub removeitem {
my $thename = shift;
my $index = indexof(\@names, $thename);
unless ($index == -1) {
splice(@names, $index, 1);
removeFromTags($nametag{$thename}, $thename);
delete $nametag{$thename};
delete $namedesc{$thename};
}
}
sub indexof {
my $arref = shift;
my $element = shift;
my $index = 0;
$index++ until $index > $#{$arref} or ${$arref}[$index] eq $element;
return ($index > $#{$arref} ? -1 : $index);
}
sub writeout {
#$ITEMNAME<name>
#$TAG<tags>
#$DESC<description>
my ($choice, $writable) = (0, 0);
print "File to write to: ";
my $fileout = <STDIN>;
chomp $fileout;
#Check for existence
if (-e $fileout) {
print "Are you sure you want to overwrite this file? (y/n) ";
$choice = <STDIN>;
chomp $choice;
if ($choice eq "y") {
#Check for writability
if (-w $fileout) {
$writable = 1;
}
}
}
else { #File doesn't exist. Write anyways
$writable = 1;
}
open FOUT, "> $fileout" or die "Unable to open file for writing: $!";
for my $entry (@names) {
print FOUT $ITEMNAME, $entry, "\n";
print FOUT $TAG, $nametag{$entry}, "\n";
print FOUT $DESC, $namedesc{$entry}, "\n\n";
}
close FOUT;
}