User`s manual
Example – Creating and Using a Phone Book
5-79
Description of Function pb_add
1. Input the Entry to Add
The pb_add function takes one argument, the Properties object pb_htable.
pb_add uses disp to prompt for an entry. Using the up-arrow (^) character as
a line delimiter,
input inputs a name to the variable entry. Then, within a
while loop, it uses input to get another line of the entry into variable
line. If
the line is empty, indicating that the user has finished the entry, the code
breaks out of the while loop. If the line is not empty, the else statement appends
line to entry and then appends the line delimiter. At the end, the
strcmp checks
the possibility that no input was entered and, if that is the case, returns.
function pb_add(pb_htable)
disp 'Type the name for the new entry, followed by Enter.'
disp 'Then, type the phone number(s), one per line.'
disp 'To complete the entry, type an extra Enter.'
name = input(':: ','s');
entry=[name '^'];
while 1
line = input(':: ','s');
if isempty(line)
break;
else
entry=[entry line '^'];
end;
end;
if strcmp(entry, '^')
disp 'No name entered'
return;
end;
2. Add the Entry to the Phone Book
After the input has completed, pb_add calls put on pb_htable with the hash
key
name (on which pb_keyfilter is called to change spaces to underscores)
and
entry. It then displays a message that the entry has been added.
pb_htable.put(pb_keyfilter(name),entry);
disp ' '
disp(sprintf('%s has been added to the phone book.', name));