Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Compare arrays containing strings

Author
Message
sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 17th Apr 2012 22:41
How do I compare two rays containing strings?

I want to compare aray$(1)
with all the elements of another array looking for a match then I want to compare array$(2) for the same and so on until I have checked my entire array.

I'm quite sure it will involve nested loops (if thats the correct term) buut I just cannot wrap my head around it.

Any help will be greatly appreciated.

Paul

pogo: http://dl.dropbox.com/u/65597655/pongo.zip
Breakout clone: http://forum.thegamecreators.com/?m=forum_view&t=185535&b=6
Lucas Tiridath
AGK Developer
16
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 17th Apr 2012 23:18
What exactly do you want to achieve here? Do you want to know if every cell in each array is the same in each array? Or whether they have any fields in common? If you say what exactly what you want to find out, I'm sure someone here will be able to give you a hand.

sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 17th Apr 2012 23:20
Ok. I have the following which crashed the compiler.



It crashes oncve a word has been removed from the rist array.
I think this is because it tries to compare the now empty array with the rest of the elements in the second array.
I need a way to get my program to go to next t after an array element has been deleted.

Any ideas?

thanks,

Paul.

pogo: http://dl.dropbox.com/u/65597655/pongo.zip
Breakout clone: http://forum.thegamecreators.com/?m=forum_view&t=185535&b=6
sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 17th Apr 2012 23:21 Edited at: 17th Apr 2012 23:22
Quote: "What exactly do you want to achieve here? Do you want to know if every cell in each array is the same in each array? Or whether they have any fields in common? If you say what exactly what you want to find out, I'm sure someone here will be able to give you a hand.
"


Sorry, I should explain.

I want to compare the elements in array 1 with the elements in array two.
If the results are the same in any element of array 1 I want to delete the element in array 1.
Basically check for fields in common and delete the common field from array 1

I hope that is clearer.

Thanks.

Paul.

pogo: http://dl.dropbox.com/u/65597655/pongo.zip
Breakout clone: http://forum.thegamecreators.com/?m=forum_view&t=185535&b=6
sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 17th Apr 2012 23:42 Edited at: 17th Apr 2012 23:43
I fixed the crash.

should be...



it still doesn't run as expected...

Guess another few runs in step through might solve it.

edited to fix code tags..

pogo: http://dl.dropbox.com/u/65597655/pongo.zip
Breakout clone: http://forum.thegamecreators.com/?m=forum_view&t=185535&b=6
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 18th Apr 2012 00:00 Edited at: 18th Apr 2012 10:47


[EDIT] Totally messed up code on phone. The format put stuff on new lines that shouldn't have been etc. Also, my apologies, I used slightly silly methodology so I've posted a mu better example in place of that mess [/EDIT]

sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 18th Apr 2012 00:06
Ok...I solved it but, I have another problem...

code looks like this and works well until the end.


most of this code is to learn new things.
Most of the print statements are for debugging.

The problem I'm having is it crashes on the final check.
You can read the code in the compareArray function and compare the output.

It 'should' check element 0 in the first array with all the elements in the second array.
if it finds a match it deletes the matching element from the first array and moves onto the next element of the first array to compare it with the elements in the second array.

at the last check it seems to find a blank element at the end of both arrays and crashes the program with an array doesn't exist or is out of bounds error.

Can anyone shed any light on what's going on?

pogo: http://dl.dropbox.com/u/65597655/pongo.zip
Breakout clone: http://forum.thegamecreators.com/?m=forum_view&t=185535&b=6
sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 18th Apr 2012 00:18 Edited at: 18th Apr 2012 00:43
looks like we both posted at thwe same time. Thank you for your response

I need to delete from array number 1 (myarray$)
Your code would run with a little modification.
I think I have fixed the problem.
I'm telling i to run until the original array count but I am diminishing the number of elements in the array by deleting them.

I will adopt your method of adding the tems which don't match into another array and using that.

Many thanks

[edit]
I removed the line to delete the elements and the program still crashes at the end. it seems there is a blank array at the end of both arrays... I need to fix this before I start adding elements to other arrays.
[/edit]

[edit2]
it now works. instead of deleting from the 1st array which was producing strange results I copied the elements i want to keep into a temp array and I'll work with that instead.

I had to knock one off the array counts (array count(myarray$())-1) to get rid of the crash.....
[/edit2]

pogo: http://dl.dropbox.com/u/65597655/pongo.zip
Breakout clone: http://forum.thegamecreators.com/?m=forum_view&t=185535&b=6
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 18th Apr 2012 10:51
Hi, sorry about last night, I made a bit of a botch. That was my first (and last) time writing out a program on my mobile phone. It reformatted some stuff and the itself was buggy (I think I must have lost track of what I was doing coz I could only see 4 lines at once ). Anyhow, I edited my post, fixed the code and it works now.

sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 18th Apr 2012 19:29
Hi nonZero,

Thanks for replying again.
no need to apologise, your code from last night put me on the right track.
I now do something similar to your second answer.

I do have a few questions though......

Q1/ in
you dim the array list every time the loop is run until you reach the end of both arrays.

Is this good practice? I realise diming the array with the correct number of elements is not possible AT THIS POINT in the code and the only work around I can think of is conveluted and quite messy (as are most of my little work arounds.)
I'm just concerned about memory usage. I'm 90% certain that when you re dimension an array it will over write the previous unstance but I would like it clarified from someone who knows better. (you ) Would adding another element on the top of the array each time one is required be a suitable alternative?

I think I understand how the rest of the code works but lets say for example...

elements 4 and 6 match.

the list() array is dimensioned (I always used to thingk DIM stood for define in memory?!?!) The first number added to the index would be 4 and the second 6?

runs and counts the arrays in list() these aer indexed 4 and 6 even though there are only 2 of them?

I'm pretty sure thats what happens but again I would appreciate it if you could confirm it for me.

The code you showed doesn't do 1 thing that I did require and thats check every element in array 1 against every element in array 2.

I have got it figured out but it's slow if a long string is entered for comparison.

I also had a strange error which crashed the compiler.
At the end of both arrays was a blank field.

I'll neaten up my code and paste it in here, maybe you can offer some advice?

Many thanks for your gelp.

Paul.

pogo: http://dl.dropbox.com/u/65597655/pongo.zip
Breakout clone: http://forum.thegamecreators.com/?m=forum_view&t=185535&b=6
sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 18th Apr 2012 19:43 Edited at: 18th Apr 2012 19:46
here is the code I have so far.



If you look through it you will see the comments about adding-1
Without the -1 the compiler crashes when the two seemingly empty elements at the end of both arrays are compared.

Any idea why the empty field?

p.s, How do I make my code coloured? I know it's something like [ code DBpro ] but I can't remember the exact syntax.

Thanks,

Paul

pogo: http://dl.dropbox.com/u/65597655/pongo.zip
Breakout clone: http://forum.thegamecreators.com/?m=forum_view&t=185535&b=6
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 18th Apr 2012 21:34
Quote: "you dim the array list every time the loop is run until you reach the end of both arrays. Is this good practice? "

Good practice is not always synonymous with pragmatism. In DBPro, DIM is equal to both DIM and the traditional "REDIM". It doesn't overwrite so you're safe. You have to UNDIM it or use the EMPTY ARRAY command (I dunno which is better) to actually clear the array. I would suggest not making a habbit of lazy coding as other languages can be way less forgiving and will require more careful memory management. Here's an example of two ways to get to the same point:


Quote: "I'm pretty sure thats what happens but again I would appreciate it if you could confirm it for me."

Yes, you are correct. A list of indexes is built and then as each element is deleted, so the rest above are displaced, requiring the counter-displacement.


Quote: "The code you showed doesn't do 1 thing that I did require and thats check every element in array 1 against every element in array 2."

Ah, I see what you mean now that I look at your code. You want to run through each item of the removal list to check each word on the word list.

Am a little busy tonight (family issues, *sigh*) so I'll have to be brief. Have started looking through your code, the -1 issue is because of the "11 in 10" as I call it (you can actually call it n+1 in n). When I have an array, there is always 1 element more than my highest index.

ARRAY COUNT returns the total number of elements in an array, 0 being counted too.

Quote: "Any idea why the empty field?"

Could be because you are using element(0). I usually pretend elem(0) isn't even there so my FOR-loops execute without hassle

Quote: "p.s, How do I make my code coloured? I know it's something like [ code DBpro ] but I can't remember the exact syntax"

"[" + "code lang=dbp" + "]" without the quotations and +'s. "lang=" can be used with certain other languages too, I think.

Will have a deeper look into you code later or tomorrow morning and see what I can come up with regarding bugs and other questions.

sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 18th Apr 2012 21:58
Thnaks again for the reply.
I've dimed a total of 14 elemensts in removeWord$ including the 0.
they run from 0 to 13.
the count in my loops starts at 0.
I'm not disputing what you say, I appreciate all the advice you have given me so far I just don't understand how running through 0 to 13 will produce the error but diming an array that runs 1 to 14 and running the same check won't.
I'm probably missing something that should be obvious but alas, I can't see it.

Your example is great. I'll modify my code to insert into the array.
No more trouble with diming an array thats too big to make sure I have room for the possible elements it could need to contain.

Many thanks,

Paul.

pogo: http://dl.dropbox.com/u/65597655/pongo.zip
Breakout clone: http://forum.thegamecreators.com/?m=forum_view&t=185535&b=6
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 19th Apr 2012 00:17
On my phone again, so cant code an example (those don't turn out well). Here's the problem:
"DIM array(14)" is 15 elements
"ARRAY COUNT(array())" returns 15
"FOR n = 0 TO 14" is 15 iterations
"FOR n = 0 TO ARRAY COUNT(array())" is 16 iterations
[the above] + "- 1" is 15 iterations.

... And now that my brain is spinning, I shall have to visit the healer for some potions ...

sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 19th Apr 2012 00:37
Thank you.
that clears it up perfectly.
Ill ditch element (0) or use it to hold the array element count as DV suggested elsewhere in the forum.

Make sure the potions you obtain from the healer have plenty of alcohol in them. For medicinal purposes of course

Many thanks nonZero.

pogo: http://dl.dropbox.com/u/65597655/pongo.zip
Breakout clone: http://forum.thegamecreators.com/?m=forum_view&t=185535&b=6
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 19th Apr 2012 21:58
You're welcome
I assume you intend to make some sort of TAD or interactive fiction engine based on your code. Anyway, here's something to take a look at in your spare time. It's nothing more than a crude code construct (alliteration! +5pts) but you may find something useful in it:



sheffieldlad
13
Years of Service
User Offline
Joined: 28th May 2011
Location:
Posted: 20th Apr 2012 21:53
Hi zero, thanks for the reply.

I will be making a text adventure with the text parser.
thats what it's for and to learn.
(Didn't want to bore people with the old 'I'm making a game' chestnut.

The code is great and I'm sure I can learn a lot from it.
Not just about arrays and loops but also producing code people can read (myself included. it's amazing how you leave something then go back to it later and think wtf does THAT do even though you wrote it )

You say in your comments at the top you are not using arrays in yout TAD engine. Can I ask how you are getting round the problem af parsing text?

Many thanks,

Paul.

pogo: http://dl.dropbox.com/u/65597655/pongo.zip
Breakout clone: http://forum.thegamecreators.com/?m=forum_view&t=185535&b=6
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 21st Apr 2012 10:41
Quote: "then go back to it later and think wtf does THAT do even though you wrote it"

How I felt about every project I've ever been lazy about (usually those I think I'll never update or use again).

Quote: "Can I ask how you are getting round the problem af parsing text?"

I'm performing an experiment on the use of csv-style strings. I have a function library that I'm building and, among others, it grabs items from these csv-alike lists. I find this more flexible and manageable than using an array as I can store lists of mixed data types and pass it to anything (useful with the clipboard when debugging). It comes at the cost of a little speed but with the usage I make of it, speed is not an issue. I spose one could argue that it *is* an array in theory - if you go to a low enough level, lol. I'm kinda unauthadox in my aproaches to things I like to experiment (just as well I'm not a nuclear physycist).

Login to post a reply

Server time is: 2024-11-21 23:30:26
Your offset time is: 2024-11-21 23:30:26