Board Logo
« Dictionary style key/value lookup example »

Welcome Guest. Please Login or Register.
Nov 20th, 2017, 10:30pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Dictionary style key/value lookup example  (Read 731 times)
CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 152
xx Dictionary style key/value lookup example
« Thread started on: Jul 20th, 2016, 08:27am »

I've been blogging about dictionary style key/value lookup recently. This morning I posted a very simple demo app that shows how to use the dictionary code I've been posting.

http://basicprogramming.blogspot.com

Feel free to use the code in your projects if you like. Feedback invited.

-Carl
User IP Logged

tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3614
xx Re: Dictionary style key/value lookup example
« Reply #1 on: Jul 21st, 2016, 02:32am »

Hello Carl
I found a few things that could be changed for a greater good ( ;) )
First, if key is missing your program returns chr$(0). The problem is I cannot paste it here - string breaks on chr$(0)! So output get pasted as
Code:
Thumb
 
instead of Code:
Thumb
chr$(0)
555-555-1234
Tom
 

That make it hard to share results.

Second, if one key is a substring of second ("last" and "last name"), you could get value of bigger key for smaller one:
Code:
call setValueByName "first", "Tom"
call setValueByName "last", "Thumb"
    call setValueByName "last name", "Piper"  'added line
call setValueByName "phone", "555-555-1234"

print getValue$("last")
print getValue$("last name")
print getValue$("blah")
print getValue$("phone")
print getValue$("first")
 

results
Code:
Piper
Piper
(chr(0))
555-555-1234
Tom
 

expected results
Code:
Thumb
Piper
(chr(0))
555-555-1234
Tom
 

In this case, calling sub collectGarbage removes short key!
Code:
before call to collectGarbage
~key~phone~value~555-555-1234~key~last name~value~Piper~key~last~value~Thumb~key~first~value~Tom
after call to collectGarbage
~key~first~value~Tom~key~last name~value~Piper~key~phone~value~555-555-1234
 


Third, it looks like call to collectGarbage reverses order of key/value pairs. It might be not important, but again, if we can make it so it will not jump back and forth, it will be easier to read/debug. It could be fixed by changing line
Code:
newDictionary$ = "~key~" + key$ + "~value~" + value$ + newDictionary$ 

in sub collectGarbage to
Code:
newDictionary$ = newDictionary$ +  "~key~" + key$ + "~value~" + value$  
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 152
xx Re: Dictionary style key/value lookup example
« Reply #2 on: Jul 21st, 2016, 06:41am »

Thanks. Yeah I know. This isn't really meant as a commercial quality library, but more as an exercise. I expected that it would be developed more as we went. :)

-Carl

on Jul 21st, 2016, 02:32am, tsh73 wrote:
Hello Carl
I found a few things that could be changed for a greater good ( ;) )
First, if key is missing your program returns chr$(0). The problem is I cannot paste it here - string breaks on chr$(0)! So output get pasted as
Code:
Thumb
 
instead of Code:
Thumb
chr$(0)
555-555-1234
Tom
 

That make it hard to share results.

Second, if one key is a substring of second ("last" and "last name"), you could get value of bigger key for smaller one:
Code:
call setValueByName "first", "Tom"
call setValueByName "last", "Thumb"
    call setValueByName "last name", "Piper"  'added line
call setValueByName "phone", "555-555-1234"

print getValue$("last")
print getValue$("last name")
print getValue$("blah")
print getValue$("phone")
print getValue$("first")
 

results
Code:
Piper
Piper
(chr(0))
555-555-1234
Tom
 

expected results
Code:
Thumb
Piper
(chr(0))
555-555-1234
Tom
 

In this case, calling sub collectGarbage removes short key!
Code:
before call to collectGarbage
~key~phone~value~555-555-1234~key~last name~value~Piper~key~last~value~Thumb~key~first~value~Tom
after call to collectGarbage
~key~first~value~Tom~key~last name~value~Piper~key~phone~value~555-555-1234
 


Third, it looks like call to collectGarbage reverses order of key/value pairs. It might be not important, but again, if we can make it so it will not jump back and forth, it will be easier to read/debug. It could be fixed by changing line
Code:
newDictionary$ = "~key~" + key$ + "~value~" + value$ + newDictionary$ 

in sub collectGarbage to
Code:
newDictionary$ = newDictionary$ +  "~key~" + key$ + "~value~" + value$  
User IP Logged

CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 152
xx Re: Dictionary style key/value lookup example
« Reply #3 on: Aug 2nd, 2016, 08:04am »

I fixed the bug about the substring false positive match that you mentioned and made a new blog post about it.

I'll make another post discussing the reordering when collecting garbage.

Thanks again,

-Carl
User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1213
xx Re: Dictionary style key/value lookup example
« Reply #4 on: Aug 2nd, 2016, 10:55am »

May I ask what garbage collection does (particularly how it relates to this application).

When I was doing mod of your mini-app in my KV Editor, I didn't understand what that part was doing.

From "A dictionary Lookup Mini Application" Code:
sub collectGarbage
  pointer = 1
  while pointer > 0
    'get the next key
    pointer = instr(dictionary$, "~key~", pointer)
    if pointer then
      keyPointer = pointer + 5
      pointer = instr(dictionary$, "~value~", pointer)
      key$ = mid$(dictionary$, keyPointer, pointer - keyPointer)
      if instr(keyList$, key$) = 0 then
        value$ = getValue$(key$)
        newDictionary$ = "~key~" + key$ + "~value~" + value$ + newDictionary$
        keyList$ = keyList$ + key$
      end if
    end if
  wend
  dictionary$ = newDictionary$
end sub
 


keyList$ had not been made GLOBAL as far as I could tell, so how could INSTR find anything in it?
Append: in Bug fixes it is still not global so my confusion remains.

Otherwise, it looks like it might be clearing redundancies in dictionary, correct?

If so, then I have that covered in my addKV function and I don't have to worry I am missing something in my KV Editor by leaving that sub or mod of it out.
« Last Edit: Aug 2nd, 2016, 11:41am by bplus » User IP Logged

B+
CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 152
xx Re: Dictionary style key/value lookup example
« Reply #5 on: Aug 2nd, 2016, 11:34am »

Sure. The keyList$ is only a locally scoped string variable which is initialized to an empty string each time collectGarbage is called.

Then goes through each key and value pair starting at the beginning of dictionary$ and copies them to the newDictionary$ and adds the key to the keyList. That way if the key is found again in dictionary$, that means that it is an older version of the key value pair, so don't add it to newDictionary. Then finally the string in newDictionary$ is assigned into the global dictionary$, replacing the original information having removed any older versions of keys.

-Carl
« Last Edit: Aug 2nd, 2016, 1:23pm by CarlGundel » User IP Logged

bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1213
xx Re: Dictionary style key/value lookup example
« Reply #6 on: Aug 2nd, 2016, 11:49am »

OH! I see my mistake in reading the code IF INSTR = 0 then build! OK

So new strings are in front, old in back. OK

Now it makes sense.

Thank you!
User IP Logged

B+
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3614
xx Re: Dictionary style key/value lookup example
« Reply #7 on: Aug 2nd, 2016, 4:29pm »

Hello Carl
while comparing old program with new I found line 67
Code:
function getKeys$(delimiter$)
  global keyCount  '<< here is line 67
 


I've seen it already made global in line 6
Code:
    global dictionary$, keyCount, lastKey$'<< here is line 6 


I do not see difference then I removed either of them (program stops if I remove both). So this is likely some remains of other code.

But I wonder: is declaring variable global inside a sub/function supposed to work?
is it supposed to work any differently then in main program?
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
ezprogramming
Guest
xx Re: Dictionary style key/value lookup example
« Reply #8 on: Aug 3rd, 2016, 01:36am »

Post deleted
« Last Edit: Sep 15th, 2016, 1:40pm by ezprogramming » User IP Logged

CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 152
xx Re: Dictionary style key/value lookup example
« Reply #9 on: Aug 3rd, 2016, 10:02am »

A global can be declared anywhere. It's probably best to declare them right up at the top of your program's code, but you have the option. ;)

-Carl

on Aug 2nd, 2016, 4:29pm, tsh73 wrote:
Hello Carl
while comparing old program with new I found line 67
Code:
function getKeys$(delimiter$)
  global keyCount  '<< here is line 67
 


I've seen it already made global in line 6
Code:
    global dictionary$, keyCount, lastKey$'<< here is line 6 


I do not see difference then I removed either of them (program stops if I remove both). So this is likely some remains of other code.

But I wonder: is declaring variable global inside a sub/function supposed to work?
is it supposed to work any differently then in main program?
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls