How to make use of 2D array

Posted 21 December 2006 - 07:48 PM

Hi folks,
I am trying to open a csv file and load into array using VB.
The below is my code on how i search for a serial no, once found, copy PWD1 & PWD3 into 2 variables using open file, read line by line.

Format of the .csv file open up using Notepad:
SERIAL,PWD1,PWD2,PWD3
352512000000000,14102953,23595170,19112674
356856000000000,15336351,19740172,29235020

I would like to make use of 2D array, open the test.csv and load data of the file to array. Then use that array for lookup.
Would like to seek help from you guys. Thank you very much!

Public Sub RetrievePWD(PWD1 As String, PWD3 As String)
Dim items() As String
Dim FName As String
Dim Buf As String
'start with AA and BB as empty until correct serial number is found
PWD1 = ""
PWD3 = ""
'set path and file name
FName = "D:\test.csv"
'open the file for input
Open FName For Input As #1
'read lines from the file
Do
'empty buffer before starting to read
Buf = ""
'read in a line and if it is blank then read the next line
Do
Line Input #1, Buf
'trim off extra spaces
Buf = Trim(Buf)
'loop should ignore empty lines but not end of file(EOF)
Loop While Buf = "" And Not EOF(1)
'If we got data check to see if it is the right serial number
If Buf <> "" Then
'split the buff into items array by the appropriate dilimeter
items = Split(Buf, ",")
If items(0) = txtUser.Text Then
'Found Serial Number
PWD1 = items(1)
PWD3 = items(3)
'Finished so exit Do Loop
Exit Do
End If
'reset array for next Split
Erase items
End If
Loop While Not EOF(1) 'loop until end of file
'done with the file so close it
Close #1
'done with items array so reset it
Erase items
End Sub

When you apply a multidimentional array, it is just grouping certain datum together.

dim Items(4,4) ' Four items each with four elements, adjust as needed
' your code
If items(0,0) = txtUser.Text Then
'Found Serial Number
PWD1 = items(0,1)
PWD3 = items(0,3)
'Finished so exit Do Loop
Exit Do
End If

This produces an array like this,
assume we are using serial number 123,124,125,126, and PWD1, PWD2, and PWD3 are all = 1 for demo purposes.

Attached image(s)

Re: How to make use of 2D array

Posted 22 December 2006 - 05:28 PM

I am not derogatory upon people that are learning. Being able to learn, that is something to be envied. I myself make mistakes, and some of them i do on purpose. I do not believe unproven theories or speculations, until i am proven that it is right and even then i like to leave myself freedom of doubt..

Sometimes i show or share my perspectives of the matter, and that only because i which to be tutored - understand more, learn. Being able to think, not just act, is one thing i share common with many people, being able to correct myself, if i should make a mistake, i share common with few.

What could agitate to learn?
If i leave out comments, someone would need to understand the code to judge, but that makes harder to be "võhik" (someone who thinks he knows, but he does not). It might seem frustrating, but if one really wants to know (to learn), he will ask, look up, look for and he will find, if not, it proves that he really did not wished to know. And in my case i like to solve tasks, that other can not or have problems with - it's like doing crosswords (i care little about if the people understand how did i solve it, i just enable them to do so and enjoy to know i can solve it, and fast).

You missed my point. When i told i do not see the reason to use 2 dimensional array, i did not mean it literally. I do not like to repeat myself, nor like when others do it, but i will make an exception so simplify what i sead by that sentence:
First do you know answers to following questions:
1) How are arrays stored in memory?
2) Can one array = many arrays?
3) What does simple code mean?

Now what did i do:
1) I made a string
2) String got a value from the file (I did not use line by line method)
3) i made an array from the string values (now i had a list of data, question is how to read it)
4) i made a function that would return values from array like it would have more dimensions Is this HARD? No. Is this FASTER? Yes Is this understandable without misunderstandings? No

Re: How to make use of 2D array

Posted 23 December 2006 - 04:37 AM

m2s87, on 22 Dec, 2006 - 05:28 PM, said:

I am not derogatory upon people that are learning. Being able to learn, that is something to be envied. I myself make mistakes, and some of them i do on purpose. I do not believe unproven theories or speculations, until i am proven that it is right and even then i like to leave myself freedom of doubt..

Sometimes i show or share my perspectives of the matter, and that only because i which to be tutored - understand more, learn. Being able to think, not just act, is one thing i share common with many people, being able to correct myself, if i should make a mistake, i share common with few.

What could agitate to learn?
If i leave out comments, someone would need to understand the code to judge, but that makes harder to be "võhik" (someone who thinks he knows, but he does not). It might seem frustrating, but if one really wants to know (to learn), he will ask, look up, look for and he will find, if not, it proves that he really did not wished to know. And in my case i like to solve tasks, that other can not or have problems with - it's like doing crosswords (i care little about if the people understand how did i solve it, i just enable them to do so and enjoy to know i can solve it, and fast).

You missed my point. When i told i do not see the reason to use 2 dimensional array, i did not mean it literally. I do not like to repeat myself, nor like when others do it, but i will make an exception so simplify what i sead by that sentence:
First do you know answers to following questions:
1) How are arrays stored in memory?
2) Can one array = many arrays?
3) What does simple code mean?

Now what did i do:
1) I made a string
2) String got a value from the file (I did not use line by line method)
3) i made an array from the string values (now i had a list of data, question is how to read it)
4) i made a function that would return values from array like it would have more dimensions Is this HARD? No. Is this FASTER? Yes Is this understandable without misunderstandings? No

Frankly you simply come across as very arrogant and condesending.
(nice stab at my spelling!)
As for "can I answer your questions?", I dirrect you to my tutorial on said subject.

Simple code?

When demonstrating to someone who dosen't know, applying single opperations per LINE when possible to make clear exactly which function is being accomplished at each step along the way. Simple programming often means more lines of code.

Re: How to make use of 2D array

I know you 2 meant well to share your abilities/skills of VB to members.

The reason i asked for 2D array as it was a suggestion given to me. Was told tat only need to open the file once and store all the data in the file to array. Some also told me storing in array will take up alot of memory. I was also given suggestion like using OLE, which i am not familiar so i decided to not to look up OLE /VBA until i make my program working. can fine tune using OLE/VBA if i have enough time to research and implement.

Re: How to make use of 2D array

(i care little about if the people understand how did i solve it, i just enable them to do so and enjoy to know i can solve it, and fast).

I believe that says it all. The purpose of this site is exactly the opposite of this statement.

Nope, opposite would be (i do not care if others understand how to do it). Synonym to your behavior is "Your looking, but your not seeing." or "Your can hear, but your not lessening.". And do not be affronted, I merely saying you are not paying attention what i am saying.

Re: How to make use of 2D array

(i care little about if the people understand how did i solve it, i just enable them to do so and enjoy to know i can solve it, and fast).

I believe that says it all. The purpose of this site is exactly the opposite of this statement.

Nope, opposite would be (i do not care if others understand how to do it). Synonym to your behavior is "Your looking, but your not seeing." or "Your can hear, but your not lessening.". And do not be affronted, I merely saying you are not paying attention what i am saying.