DIMing an array with words

General FreeBASIC programming questions.
axipher
Posts: 891
Joined: Dec 27, 2005 16:37
Location: Sudbury,Ontario
Contact:

DIMing an array with words

Postby axipher » Jan 12, 2006 15:42

This is very similar to my color array problem. What I want to do is create lists of words and store them in an array. Something like:

Code: Select all

DIM as UINTERGER list(1)
list(0) = "hi"
list(1) = "hello"


I'm sure this won;t work because words are strings and contain mre then 1 byte or whatever UINTEGER does. What would I dim the array as for strings?
Imortis
Moderator
Posts: 1744
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Postby Imortis » Jan 12, 2006 16:18

Code: Select all

Dim as String list(1)
list(0) = "hi"
list(1) = "hello"


I think that's what your asking... Not sure though...
MystikShadows
Posts: 612
Joined: Jun 15, 2005 13:22
Location: Upstate NY
Contact:

Postby MystikShadows » Jan 12, 2006 16:29

If you really want numbers you can also create (for colors) an ENUM type

Code: Select all

ENUM ColorList
     Black
     Blue
     Green
     Cyan 
     Red
     Magenta
     Brown
     Grey
     DarkGrey
     LightBlue
     LightGreen
     LightCyan
     LightRed
     LightMagenta
     Yellow
     White
END ENUM


In this case, Black = 0, Blue = 1, etc etc... You can change that default value by adding a = <Value> after the name.

For example:

Code: Select all

ENUM TimeIntervals
     Yearly      = 1
     SixMonths   = 2
     FourMonths  = 3
     ThreeMonths = 4
     TwoMonths   = 6
     Monthly     = 12
     BiMonthly   = 24
     TwoWeeks    = 26
     Weekly      = 52
END ENUM


Then when you create a variable, you can assign it like so:

Code: Select all

DIM ForegroundColor AS ColorList
DIM BackgroundColor AS ColorList
DIM TimeLaps        AS TimeIntervals

ForegroundColor = Grey   ' any of the other names in the ENUM
BackgroundColor = Black  ' any of the other names in the ENUM
TimeLaps        = Monthly



EDITED: Unless you mean the Associative Array data structure? where you store an array and you can retrieve any element by an associated name rather than an array index?
axipher
Posts: 891
Joined: Dec 27, 2005 16:37
Location: Sudbury,Ontario
Contact:

Postby axipher » Jan 13, 2006 13:50

What I was thinking was having a Safe list and an Ignore list, then when a message is recieved or a name inputted, it compares it to the words in a specified list, can you do this?
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » Jan 13, 2006 14:20

Code: Select all

option explicit

function FindWord( wordList() as string, word as string ) as integer
    if( len( word ) = 0 ) then return 0
   
    dim as integer it = 0
    while( it <> ubound( wordList ) )
        if( wordList( it ) = word ) then return -1
        it += 1
    wend
    return 0
end function

function StringArrayExample( ) as integer
    const MaxWords as uinteger = 3

    dim as string SafeList( MaxWords - 1 ) => { _
        "ribeye", _
        "t-bone", _
        "prime rib" _
    }
    dim as string IgnoreList( MaxWords - 1 ) => { _
        "broccoli", _
        "cauliflower", _
        "brussel sprouts" _
    }

    dim as string userWord
    print "Enter a word: " ;: input userWord
   
    if( FindWord( SafeList(), userWord ) ) then
        print userWord ; " found in the safe list."
    elseif( FindWord( IgnoreList(), userWord ) ) then
        print userWord ; " found in the ignore list."
    else
        print userWord ; " not in either list."
    end if

    sleep : return 0
end function

    end StringArrayExample()
axipher
Posts: 891
Joined: Dec 27, 2005 16:37
Location: Sudbury,Ontario
Contact:

Postby axipher » Jan 14, 2006 4:22

thanks alot stylin, that's exactly what I was looking for. If anyone can find a shorter way then don;t hesitate to post it.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » Jan 14, 2006 6:10

Why would you be looking for a shorter way? I'm going to assume by 'shorter' you mean 'faster'.

At this point, if you cannot concieve of a faster method of doing this on your own then you're probably trying to optimise too early (premature optimisation: don't worry, it happens to the best of us). Use what works to start, then later - after profiling and proving that a certain section or algorithm is a bottleneck that needs rework - you can refactor (optimise) to better suit your needs, whether it be efficiency, robustness, reusability, etc.. Until you have that proof, looking for faster - or shorter (which doesn't even make sense) - code is a waste of time which could be better spent implementing the rest of your design.

My 2c.
axipher
Posts: 891
Joined: Dec 27, 2005 16:37
Location: Sudbury,Ontario
Contact:

Postby axipher » Jan 14, 2006 14:17

Well I found a way to loop it, but that's about it so far, I'm trying to change it to a FUNCTION that accepts parameters like SUB's do, ie. FUNCTION (text$, wordlist), then I can use If and If NOT( I think I can do that).

Nevermind, It already is like that in FUNCTION form. Thanks again.

Return to “General”

Who is online

Users browsing this forum: No registered users and 12 guests