A new section for FreeBasic on The Joyful Programmers forum

General discussion for topics related to the FreeBASIC project or its community.
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by owen »

this is what im watching in an effort to implement associative arrays in my entry for the contest using freebasic.

https://www.youtube.com/watch?v=UycDHbc ... freload=10 which is part 1. i am unable to watch part 2 because there is an add at the start and my connection to the internet is too slow to get past it.

and because i really don't know what a hash table is, im watching this

https://www.youtube.com/watch?v=h2d9b_nEzoA

and this (ps. MIT is awesome)

https://www.youtube.com/watch?v=0M_kIqhwbFo (please fb, i don't want to have to switch to python)

but as i've posted the qb forum i will first attempt the poor man's approach using arrays rather then hash tables, dictionaries, binary trees, etc...
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by srvaldez »

owen wrote:this is what im watching in an effort to implement associative arrays in my entry for the contest using freebasic.

https://www.youtube.com/watch?v=UycDHbc ... freload=10 which is part 1. i am unable to watch part 2 because there is an add at the start and my connection to the internet is too slow to get past it.
why not install an add blocker?
I use addblock plus https://adblockplus.org, highly recommended.
bplus
Posts: 56
Joined: May 01, 2017 15:57

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by bplus »

srvaldez wrote:
owen wrote:this is what im watching in an effort to implement associative arrays in my entry for the contest using freebasic.

https://www.youtube.com/watch?v=UycDHbc ... freload=10 which is part 1. i am unable to watch part 2 because there is an add at the start and my connection to the internet is too slow to get past it.
why not install an add blocker?
I use addblock plus https://adblockplus.org, highly recommended.
I saw this tip before, got distracted from trying it. Saw this again tonight, now I have it. The world just got a little more sane! Thanks!!!
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by owen »

thanks for the idea of running an ad blocker but my laptop is old and has a hard enough time running xp.

so here is what im working on regarding associative arrays for the contest in addition to what I have so far. how i'm going to fit into 100 loc i don't know yet but we'll see.

note: in an effort to pay tribute to freebasic i try to stick with the same command names that fb uses but becuase associative arrays are not built in (so to speak) i will have to add some extra key words such as "associative array" "all records", "record", "where" along with the > sign.
a program that will interpret the following:

Code: Select all

associative array contact(7,2)

contact("name")="owen reese"
contact("cell phone number")="4076558537"
contact("city")="kissimmee"
contact("state")="florida"
contact("web site")="fbcadcam.com"
contact("occupation"="truck driver"
contact("hobbies")="programming in freebasic","prime number research"

contact("name")="eseer newo"
contact("cell phone number")="7358556704"
contact("city")="eemmissik"
contact("state")="adirolf"
contact("web site")="moc.macdacbf"

print contact>all records
print contact>record where "name"="eseer newo"
print contact>"name","eseer newo","web site"

freebasic code

Code: Select all

'Simple Interpreted Basic"
Dim As String temp, variable_name(0), array_name(0)
Dim as integer variable_value(0), variable_count, array_1d_value(0,0), array_1d_count
Open "source_code.txt" for input as #1
Do While Not EOF(1)
   Line Input #1, temp
   If Lcase( Left(temp,17) ) = "associative array" then
      ...code pending
   End If
   If Lcase( Left(temp,8) ) = "variable" then
      variable_count+=1
      Redim Preserve variable_value(variable_count)
      Redim Preserve variable_name(variable_count)
      variable_name(variable_count) = Mid(temp,10)
   End If
   If Lcase( Left(temp,5) ) = "array" then
      array_1d_count+=1
      Redim Preserve array_name(array_1d_count)
      Redim Preserve array_value(array_1d_count,0)
      array_name(array_1d_count) = Mid(temp,7)
   End If
   If Lcase( Left(temp,3) ) = "set" then
      'here is where we would need to find the variable name or array name but first I need to parse temp string
      'extracting the variable name from the line of text.
      'we need to find the the first space " " after the variable name.
      tempvn=Mid(temp,5,Instr(6,temp," ")-5)
      For i = 1 to variable_count
         If tempvn = variable_name(i) then
            variable_value(i)=Val(Mid(temp,5+Len(tempvn)))
            Exit For
         End If
      Next
      For i = 1 to array_count
         If tempvn = variable_name(i) then
            array_1d_count+=1
            'now here is where would need to find the array length
            array_values=Mid(temp,5+Len(tempvn))
            array_length+=1
            For i = 1 to Len(array_values)
               if Mid(array_values,i,1)=" " then array_length+=1
           Next
            If array_length > max_array_length then max_array_length = array_length
            Redim Preserve array_1d_value(array_1d_count,max_array_length)
            'now we can set the values
            c=0
            temp_array_value=""
            For i = 1 to Len(array_values)
               temp_array_value += Mid(array_values,i,1)
               if Mid(array_values,i,1)=" " then
                  c+=1
                  array_1d_value(array_1d_count,c)=val(temp_array_value)
                  temp_array_value=""
               end if
           Next
           Exit For
        end if
      Next
   End If
   If Lcase( Left(temp,5) ) = "print" then
      ...
   end if
   If Lcase( Left(temp,4) ) = "copy" then
      ...
   end if
   If Lcase( Left(temp,3) ) = add" then
      ...
   end if
   ...here is where i would need to catch thing like:
   ...contact("name")=
loop
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by owen »

I want somebody who is pro freebasic to work with me on this contest.
Maybe a better way to say it is "I want fb to win this contest rather than python etc..."

I think it's true what the MIT professor said; my translation, "programming languages gotta have associative arrays at a minimum now a days"
And I think this is the case because new programmers are learning to program in a "natural" way. And hasn't this been the case all along. Wasn't that the very concept of basic itself?

so If somebody that's "pro freebasic" wants to work with me on this contest entry just give me a shout and you can have the trophy.
rpkelly
Posts: 52
Joined: Sep 03, 2016 22:36

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by rpkelly »

See if this FB class I wrote does anything to assist you.

http://www.planetsquires.com/protect/fo ... pic=3984.0
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by owen »

oh, wow, thanks for sharing that. adds keys to associative 1d arrays for all fb variable types. excellent example and encryption too.
where is #Include "afx/CWstrDic.inc"?
rpkelly
Posts: 52
Joined: Sep 03, 2016 22:36

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by rpkelly »

owen wrote:oh, wow, thanks for sharing that. adds keys to associative 1d arrays for all fb variable types. excellent example and encryption too.
where is #Include "afx/CWstrDic.inc"?
Jose Roca's excellent classes for FB. What's in your wallet?

http://www.planetsquires.com/protect/fo ... board=39.0
bplus
Posts: 56
Joined: May 01, 2017 15:57

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by bplus »

I am thinking that owen (and bplus) might also benefit from a proper Split routine (say a program line into an array) and Eval function (able to do strings as well as math) if any are handy. ;-)

I mean Python has this magic word called import that allows just about anything through the door.
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by owen »

here is something that is helping me understand the concept of associative arrays.
http://www.scriptbasic.org/forum/index. ... 170.0.html

found an interesting comment from the author or scriptbasic: "To be hones I would have not created ScriptBasic it if I knew Python existed."
http://www.scriptbasic.org/forum/index. ... ,81.0.html
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by relsoft »

Why not implement your associative array under the hood with a hashmap?

That what I did here:
https://github.com/relminator/AnyaBasic


Hello guys, long time no see. Thanks for the help. :)
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by owen »

wow, you're back. how are you relsoft?

thanks i'll look at your hashmap. but this is for a 100 loc contest so im restricted to using something short. any how i needed to know all about associative arrays. i didn't even know they were a thing to know about until a few days ago.
Last edited by owen on Jun 08, 2017 9:34, edited 1 time in total.
owen
Posts: 555
Joined: Apr 19, 2006 10:55
Location: Kissimmee, FL
Contact:

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by owen »

Here is what relsoft was referring to. i'm looking at the code to see how it was done:

usage example

Associative Arrays(They are technically hashmaps).
Associative arrays only accepts Strings as keys in a Key:Value pair. Values can be anothing though.
In fact values can be arrays, types or functions. :)

Code: Select all

    start
       Type Vec2D
       start
           x = 20
           y = 30
       end
       Vec2D v
       v.x = 45
       v.y = 23
       Var a[10][10]
       a[1][1] = "AnyaBasic"
       var map{:}               # This is how you declare Associative arrays #
       map{"rel"} = 999         # Put a number value #
       map{"anya"} = v          # Put a user defined type value #
       map{"Lilytte"} = a       # A 2D array this time #
       var x 
       x = map{"rel"}           # Retrieve a value #
       x = map{"anya"}
       print(x.x)
       print(x.y)
       var y = map{"Lilytte"}
       print(y[1][1])
       print(containsKey(map,"anya"))   # Check if a key is contained in the associative array #
       print(sizeOf(map))               # Print its size #
    end
relsoft
Posts: 1767
Joined: May 27, 2005 10:34
Location: Philippines
Contact:

Re: A new section for FreeBasic on The Joyful Programmers forum

Post by relsoft »

I'm alright. Pretty much my wife's nurse these days.

Good luck with the compo.

I'm always on Facebook since it's free(though without pics and videos) in my country.
Post Reply