2 array questions

New to FreeBASIC? Post your questions here.
TriumphRacer
Posts: 164
Joined: Aug 28, 2005 21:06
Location: irwin, pa
Contact:

2 array questions

Post by TriumphRacer »

I need some help initializing an array containing strings.

Here's the code:

DIM steady_job(1 TO 8) AS STRING => ("yes", "yes", "no", "no", _
"yes", "yes", "no", "no")


When I attempt to compile, I get:

compiling: idbcxxxx.bas -o idbcxxxx.asm

idbcxxxx.bas(75) : error 7: Expected ')', found: ','

DIM steady_job(1 TO 8) AS STRING => ("yes", "yes", "no", "no", _
^

Any idea what I'm doing wrong?

Next question. I'm attempting to add some statistical analysis to my engine program. The above is just a test case to test my math. Here's what I need to do. At _compile_ time, I have no idea how many arrays the user will need, whether the elements of the arrays will be numeric or string or mixed, or how many elements each array will have. How can I make the arrays *VERY* dynamic to cover just about all eventualities?

Thanks again in advance.
Bill
;-))
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Post by v1ctor »

To initialize arrays use {}'s, ()'s are used with UDT's.
arenth
Posts: 511
Joined: Aug 30, 2005 6:22

Post by arenth »

As far as dynamic arrays REDIM works rather nicely.

For instance

DIM steady_job() AS STRING

DIM jobCount as LONG

jobCount = 0

DO
--Something about handing user input--

'Add new job
REDIM PRESERVE steady_job(jobCount)
steady_job(jobCount) = userInput
jobCount +=1

LOOP

REDIM means REDIMension, PRESERVE will tell freebasic to non-destructively resize the array. This means any existing data will remain up to the length of the new array.
TriumphRacer
Posts: 164
Joined: Aug 28, 2005 21:06
Location: irwin, pa
Contact:

array questions

Post by TriumphRacer »

Whoops! Mark up question #1 to monitor resolution (not to mention age and eyesight ;-)) Went back and rechecked help files and found curly braces not paran. Had to squint.

In regards to 2nd question, I'm not sure if it was answered or not. Mayhaps I should restate.

When I compile the program, I will have no idea how many arrays the user will need. Could be 3 or 4, could be 15 or 20.

Neither will I have any idea how many elements each array will have. Maybe 2 or 3, maybe 75 or 80.

Nor will I have any idea whether or not the array elements will be numeric or string. In my math test example, I'm using text but it could just as well be a 3 or a 3.1419572 or a yes or no. I need to make it as flexible as possible.

Can I make the array name a variable? Then tack a number on the end: e.g. array1, array2, array3, ... arrayn.

Anyone familiar with the ID3 algorithm? That's what I'm trying to implement.

Thanks again.

Bill
VirusScanner
Posts: 775
Joined: Jul 01, 2005 18:45

Post by VirusScanner »

I'd read about pointers and using allocate/deallocate/reallocate. That way nothing is allocated in memory until you need it, and you can delete it if it isn't needed anymore.
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Re: array questions

Post by 1000101 »

TriumphRacer wrote:In regards to 2nd question, I'm not sure if it was answered or not. Mayhaps I should restate.

When I compile the program, I will have no idea how many arrays the user will need. Could be 3 or 4, could be 15 or 20.

Neither will I have any idea how many elements each array will have. Maybe 2 or 3, maybe 75 or 80.

Nor will I have any idea whether or not the array elements will be numeric or string. In my math test example, I'm using text but it could just as well be a 3 or a 3.1419572 or a yes or no. I need to make it as flexible as possible.

Can I make the array name a variable? Then tack a number on the end: e.g. array1, array2, array3, ... arrayn.
Basically what you want is a linked list. This way you can allocate new "variables" as well and keep track of them in a simple manner. You may also want to look into hashing algorythms, which, may provide a better solution then linked lists.
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Post by yetifoot »

i was wondering this recently

Code: Select all

DIM steady_job(8) AS STRING => {"yes", "yes", "no", "no", _
"yes", "yes", "no", "no"}
gives me error
cannot initialize dynamic arrays
so i had to do something like this

Code: Select all

DIM steady_job(8) AS ZSTRING * 20 => {"yes", "yes", "no", "no", _
"yes", "yes", "no", "no"}
although i didnt like this because in a large one there will be wastage due to all elements being len 20
VirusScanner
Posts: 775
Joined: Jul 01, 2005 18:45

Post by VirusScanner »

This works for me:

Code: Select all

dim as zstring ptr test(0 to 1) => {strptr("hi"), strptr("not another one")}

print *test(0), *test(1)
sleep
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: 2 array questions

Post by D.J.Peters »

TriumphRacer wrote:Next question. ... Here's what I need to do. At _compile_ time, I have no idea how many arrays the user will need, whether the elements of the arrays will be numeric or string or mixed, or how many elements each array will have. How can I make the arrays *VERY* dynamic to cover just about all eventualities?
build your own dynamic variant data type (in size too).

If you need more instructions let me know.

Joshy
Image
Last edited by D.J.Peters on Dec 13, 2005 6:35, edited 1 time in total.
DrV
Site Admin
Posts: 2116
Joined: May 27, 2005 18:39
Location: Midwestern USA
Contact:

Post by DrV »

You could also do:

Code: Select all

DIM steady_job(8) AS ZSTRING ptr => {strptr("yes"), strptr("yes"), strptr("no"), strptr("no"), _
strptr("yes"), strptr("yes"), strptr("no"), strptr("no")} 
EDIT: oops, looks like some people beat me to it already :)
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Post by v1ctor »

Btw, just @ is enough, literal strings are zstring's so STRPTR has the same effect as VARPTR (or the @ address of operator).

Code: Select all

DIM steady_job(8) AS ZSTRING ptr => {@"yes", @"yes", @"no", @"no", @"yes", @"yes", @"no", @"no"}
TriumphRacer
Posts: 164
Joined: Aug 28, 2005 21:06
Location: irwin, pa
Contact:

array questions

Post by TriumphRacer »

[quote]
build your own dynamic variant data type (in size too).

If you need more instructions let me know.

Joshy
[/quote]

I'd be *very* interested in some help on this. As I've stated before, I'm not a pro programmer, just an amateur and I think I'm getting in over my head but the concept is a good one.

Thanks to all for bearing with a neophyte!

Bill
;-)))
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

Hello TriumphRacer,
it exist many ways to implement an varaiant data type and i can post a version next weekend but it will help if you tell what is the main usage for example dynamic arrays of varaiant to expand database reading or what ever.

for what do you need it?

Joshy
Image
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Re: 2 array questions

Post by stylin »

TriumphRacer wrote:How can I make the arrays *VERY* dynamic to cover just about all eventualities?
With generic programming paradigms - in C-style languages (C, Pascal, etc.) this involves writing MACROs. You parameterize the type of element you need to store in the array, and let the compiler do the rest - duplicating virtually identical code for any and all element types you wish to include.

I'm in the process of writing a set of tutorials on this subject, but here is an incomplete implementation of a generic array, which can be expanded to make it dynamically resizable upon adding/removing elements. It might be enough to get you started.
syzygy
Posts: 12
Joined: Nov 25, 2005 8:58

Post by syzygy »

To answer your problem, I recommend setting up an array of arrays. If you do that dynamically, you can resize the number of data sets you want to evaulate by resizing the "master array".

Each array within the master array could be composed of unions. The unions in turn hold members, one for each of the data types (string, int, real, ...) you want to examine. For each of the arrays, record the array's data type and the number of entries in the data set, and you should be halfway there.

I recommend checking out the TYPE, UNION and DIM/REDIM entries in the online help.

Cheers, and good luck,

syzygy
Post Reply