9 Replies - 23939 Views - Last Post: 26 December 2006 - 09:08 PM Rate Topic: -----

#1 Trazz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-December 06

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


This post has been edited by Trazz: 21 December 2006 - 07:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to make use of 2D array

#2 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: How to make use of 2D array

Posted 22 December 2006 - 08:59 AM

I wounder whay people make thait code SO complex.

Avoid using multidimentional arrays, if you can. B) Well i did't get the point whay 2 dimentional array is needed. But if you want you can implemant it. :sleepy:

I would use following code:
Function sisu(nimi$) As String
	Dim arv As Integer
	arv = FreeFile
	Open nimi$ For Input As #arv
		sisu = Input$(LOF(arv), arv)
	Close #arv
End Function
Function vaja(m$(), mida%, Optional nr% = 1) As String
	Select Case mida
	   Case 0
		  vaja = m(3 + nr) & "," & m(4 + nr) & "," & m(5 + nr) & "," & "," & m(6 + nr)
	   Case 1
		  vaja = m(3 + nr*4)
	   Case 2
		  vaja = m(4 + nr*4)
	   Case 3
		  vaja = m(5 + nr*4)
	   Case 4
		  vaja = m(6 + nr*4)
	End Select
End Function
Sub main()
Const KÕIK = 0, _
SERIAL = 1, _
PWD1 = 2, _
PWD2 = 3, _
PWD3 = 4

Dim tekst As String
Dim pikk As Integer

tekst = sisu("D:\test.csv")
kogu = Split(tekst)

vaja = tagasta(KÕIK, 2)
End Sub

This post has been edited by m2s87: 22 December 2006 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 KeyWiz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 438
  • Joined: 26-October 06

Re: How to make use of 2D array

Posted 22 December 2006 - 09:54 AM

View Postm2s87, on 22 Dec, 2006 - 08:59 AM, said:

I wounder whay people make thait code SO complex.


I wonder why you are so derogitory about people who are just learning. MAYBE the reason for a TWO DIMENTIONAL ARRAY was so that the student LEARNS how to use a multidimentional array.

I believe, a little explanation is needed here. See my tutorial Arrays Explained in Visual Basic Tutorials

http://www.dreaminco...wtopic20197.htm

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.

the array is

Attached image(s)

  • Attached Image

This post has been edited by KeyWiz: 22 December 2006 - 11:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

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
:blink: Is this HARD? No. Is this FASTER? Yes Is this understandable without misunderstandings? No :crazy:
Was This Post Helpful? 0
  • +
  • -

#5 KeyWiz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 438
  • Joined: 26-October 06

Re: How to make use of 2D array

Posted 23 December 2006 - 04:37 AM

View Postm2s87, 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
:blink: Is this HARD? No. Is this FASTER? Yes Is this understandable without misunderstandings? No :crazy:


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.

Some of us had to learn to crawl before we learned to run.
Was This Post Helpful? 0
  • +
  • -

#6 Trazz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-December 06

Re: How to make use of 2D array

Posted 25 December 2006 - 11:14 PM

hi KeyWiz & m2s87 .. tks for the replies.
I'm sorry tat my posting caused some conflicts b.w the 2 of you.

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.

Thank you
Was This Post Helpful? 0
  • +
  • -

#7 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: How to make use of 2D array

Posted 25 December 2006 - 11:22 PM

Trazz, there's no need to apologize.
Was This Post Helpful? 0
  • +
  • -

#8 KeyWiz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 438
  • Joined: 26-October 06

Re: How to make use of 2D array

Posted 26 December 2006 - 08:39 AM

View Postm2s87, on 22 Dec, 2006 - 05:28 PM, said:

(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.
Was This Post Helpful? 0
  • +
  • -

#9 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: How to make use of 2D array

Posted 26 December 2006 - 01:12 PM

View PostKeyWiz, on 26 Dec, 2006 - 08:39 AM, said:

View Postm2s87, on 22 Dec, 2006 - 05:28 PM, said:

(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. :huh:
Was This Post Helpful? 0
  • +
  • -

#10 KeyWiz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 438
  • Joined: 26-October 06

Re: How to make use of 2D array

Posted 26 December 2006 - 09:08 PM

View Postm2s87, on 26 Dec, 2006 - 01:12 PM, said:

View PostKeyWiz, on 26 Dec, 2006 - 08:39 AM, said:

View Postm2s87, on 22 Dec, 2006 - 05:28 PM, said:

(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. :huh:



Do you realize how moronic all this is? I'm done, goodbye.
Was This Post Helpful? 1

Page 1 of 1