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.

AppGameKit Classic Chat / Simple compare two Arrays - Doesnt seem to work?

Author
Message
Rudders
12
Years of Service
User Offline
Joined: 20th Jan 2012
Location: Oxfordshire UK
Posted: 14th Feb 2013 10:25
Hi Gurus,

Ok, what am I doing wrong? I have two Arrays, both defined with the same Type and containg the same data. I want to compare data from an element in one Array to find matching data in the other Array... Sounds straight forward!

The Arrays are declared as follows:


type myType
barcode as string
bcodeqty as string
bcodeuom as string
plu as string
endtype
dim myArray[rows] as myType
dim myArray2[rows] as myType


I load myArray with data and copy every record and cell to myArray2

I now want to compare myArray[selected_record].barcode with myArray2[any-record].barcode to find a match... I use the following sub-routine:


getrec:
For any_record = 1 to last_record
If myArray2[any_record].barcode = myArray[selected_record].barcode
Exit
Endif
Next
Return


This should return the record number that matches in the variable selected_record. BUT IT DOESN'T

The For... Next runs through the entire Array (3874 records) without ever finding a match! It appears that the simple statement
'If myArray2[any_record].barcode = myArray[selected_record].barcode' is never TRUE yet the data in each Array is the same...

Help me please!
TrezSoft
AGK Tool Maker
12
Years of Service
User Offline
Joined: 8th Nov 2012
Location: UK
Posted: 14th Feb 2013 11:31
Are your arrays starting at 1 or 0 ? the current loop starts at 1.

Do you get the same result if you hard code the array tests to know values ?

If myArray2[10].barcode = myArray[10].barcode
Rudders
12
Years of Service
User Offline
Joined: 20th Jan 2012
Location: Oxfordshire UK
Posted: 14th Feb 2013 11:52
I am starting at 1, ignoring 0

I have since written a simple version with arrays of [10,2] and dim as String with the same data in each array... AND IT WORKS!

I have even copied the data from the first array in reverse order into the second array and carried out the same test checking that array1[any,1] can find array2[any,2] and it still works fine!

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 14th Feb 2013 11:54 Edited at: 14th Feb 2013 12:06
Quote: "This should return the record number that matches in the variable selected_record. BUT IT DOESN'T"

Subroutines don't 'return' values, the return keyword refers to returning to the point at which the call to the subroutine was made, it doesn't return a value. If your arrays are global (highly likely) then simply change it to a function:

function compare()
index = 0
For any_record = 1 to last_record
If myArray2[any_record].barcode = myArray[selected_record].barcode
index = any_record
Exit
Endif
Next
endfunction index

Edit- you could also add in the extra variable into the subroutine and access it outside the subroutine but that may pose scope problems. I hope you get it working!

Rudders
12
Years of Service
User Offline
Joined: 20th Jan 2012
Location: Oxfordshire UK
Posted: 14th Feb 2013 12:33
Hi Thanks, yes I understand the point re 'return' and it was just the variable in the FOR / NEXT loop I was using globally.

I should really use a Function for the compare it would be neater.

Cheers
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 14th Feb 2013 13:26
Is the = here actually able to do the comparison? I doubt it, but I'm probably wrong!

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
Rudders
12
Years of Service
User Offline
Joined: 20th Jan 2012
Location: Oxfordshire UK
Posted: 14th Feb 2013 13:35
PROBLEM SOLVED! NOT THE FAULT OF AppGameKit BUT MINE...

The actual problem with my program was right back at the Array loading... I am using GetStringToken to load the array from a CSV file, my data has records of 4 fields eg: 100023435,1,,100345 NOTE that field 3 has no data between the commas (,,)

What is happening is that the array[any,3] is not being loaded with any data, infact this is being skipped and data 4 (100345 in above example) is being loaded into array[any,3] and not array[any,4]

This may be the subject of a new thread if I cannot find a solution, unless anyone here knows why?

Cheers
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 14th Feb 2013 14:49
Quote: "Is the = here actually able to do the comparison? I doubt it, but I'm probably wrong!"

This is one of the things I definitely don't like about Basic (which is about the lowest on my list of languages). It actually does use a single '=' to check for equality in logical statements.

When I converted my Tier 1 to Tier 2, this was a royal pain to make sure to catch all of the cases.

Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master
Rudders
12
Years of Service
User Offline
Joined: 20th Jan 2012
Location: Oxfordshire UK
Posted: 14th Feb 2013 17:33
Just to update on the CSV file read proble using GetStringToken..

You can not use GetStringToken to read a CSV file if any of the data is null, meaning there is nothing between the commas as in this example: '011213456,100056,,,SOME TEXT' The data fields 3 and 4 do not have anything in them and will be totally ignored by the GetStringToken call returning the value of 3 instaed of the correct value 5.

SOLUTION!

I have written my own CSV file reader which declares a dynamic array with the required number of rows of data and cells for each row based on the CSV file itself. The routine also 'reads' the delimiter "," and places cell data in the correct location in the array even if null...

Anyone wants the code just let me know.

Happy man again..

Login to post a reply

Server time is: 2024-11-24 08:09:10
Your offset time is: 2024-11-24 08:09:10