?
Solved

loading the data from a text file into an array?

Posted on 2003-03-05
12
Medium Priority
?
175 Views
Last Modified: 2010-05-01
how do i load the data stored in a text file, into an array in Visual Basic?  I am currently working on a project that will convert english phrases to french.

i am to create a program module....
create suitable storage for the language data
code the form to load the language data into storage immediately as well as create suitable labes and text boxes to allow phrases to be shown.

Can i use global arrays for this?

If so, how?

thanks

eokorie
0
Comment
Question by:eokorie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +4
12 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 8072028
Option Explicit
'This is on top of your project
Dim Fun(1 To 100, 1 To 100) As String

Private Sub Form_Load()
fun(1,1) = "Hi"
fun(1,2) = "Hi in french"
End Sub
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 8072069
sorry:
dim Free as integer
dim i as integer

free = Freefile

i = 1

Open "C:\French.txt" for input as #Free

Do while not Eof(free)

  fun(i,i) = "Hi"
  fun(i,i + 1) = "Hi in french"

i = i + 1

loop

Close #free

Something like this
0
 
LVL 2

Expert Comment

by:Jacamar
ID: 8072298
ok, to store the data, i suggest this format

Hello, bonjour,
How are you, cava,
ect
just make sure that the english and french sayings are separated by a comma.  Then place the text file in the same folder as your application.  Now place the following code in whatever procedure you want it in.

dim Filename as string
dim stTranslate() as string

Filename = app.path + "\French.txt"

Open Filename for Input as #1

Do while not EOF(1)

  inCounter = inCounter + 1
  Redim Preserve stTranslate(1 to inCounter, 1 to 2)
  Input #1, stTranslate(inCounter, 1)
  Input #1, stTranslate(inCounter, 2)

Loop

Close #1




Now,  this will input all your values in to your array.  they will be kept as stTranslate (wordnumber, 1 or 2).  All the ones will be english, and all the twos will be french.  

I hope this is what you are looking for.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:vikram_c90
ID: 8073155
There is a very simple way to do this. First of all please answer the following questions:

1)R u going to create the text file urself and create text data in it??
2)Will u save it as a text (.txt) file??

if so, it is very simple.
Create the text file in delimited format eg:
Hello~abcd~Hi~efgh~where~ghyt
Here I have used the Tild "~" sign as a delimiter. All that u need to do is use the Split function in VB. I am giving u the code below:

Dim fileHandle, FP
Dim strData As String
Dim arrData() As String
Set fileHandle = CreateObject("Scripting.filesystemobject")
Set FP = fileHandle.OpenTextFile(<ur text file name>, 1) strData = FP.ReadAll
arrData = Split(strData, "~")
Set fileHandle = Nothing
Set FP = Nothing
Exit Sub

Now the array arrData will contain all the individual words from the text file. All even index valu of the array will contain ur english words and al odd index values will contain your French words. U will need to add a reference to Microsoft Scripting Runtime to ur project. Using this object is much better than using the VB default file handling by the Open method.
I hope this solves ur problem. Please let me know.
0
 
LVL 2

Expert Comment

by:keenez
ID: 8074320
Eokorie,

If you want to store each line as an element of an array, you would most likely have to keep redim'ing a variable .... may I suggest something like this instead

Dim fInt as integer
Dim table as New Collection
fint = freefile
open file for input as #fint

while not eof(fint)
    Line Input #fInt, textLine
    table.add textLine, textLine

wend

You can then scroll through the lines

for i = 1 to table.count

    debug.print table.item(i)
next
0
 

Accepted Solution

by:
zfiexpert earned 400 total points
ID: 8076005
I would dimmension 2 arrays, one for the english version and one for the french version. Then open the file with:

Open "c:\language.txt" for Input As #1

For A = 0 to EOF(1)
Input #1, EnglishArray(A)
Input #1, FrenchArray(A)
Next A

Close #1

This makes using your array for display and input eaisier because the array number will be the same for the English and French strings.

When you create your text file it will look like this:

yes
oui
what
quoi
why
pourquoi

(You have to make this file in notepad or something before you can run the program.)

Simplify.

-Shane
ZFi Enterprises
0
 

Expert Comment

by:zfiexpert
ID: 8076029
<CONTINUED>

In that For A loop after the two input statements add this:

Redim Preserve FrenchArray(A)
Redim Preserve EnglishArray(A)

This will add to the array.

-Shane
ZFi Enterprises
0
 

Expert Comment

by:zfiexpert
ID: 8076035
Jeeze, BEFORE THE TWO INPUT STATEMENTS. There.
0
 

Author Comment

by:eokorie
ID: 8113858
Thanks to everyone for their input....  but is there way i could do this?

supposing i have two text boxes in visual basic.  in one of them, i will type in an english word and the other textbox displays the french equivalent at the touch of a button...

any ideas?

eokorie
0
 
LVL 2

Expert Comment

by:Jacamar
ID: 8114264
load your dictionary of french and english words in to your array when you load the form.  

When you type in the english word.  Have your program find the english word in your array:
  stTranslate (inCounter,1)  - (the one is indicating english)

and have the program output the french word

  stTranslate (inCounter, 2)

To make it faster, You could load up arrays for each letter so that if your english word starts with an "A" then it will only look for that word in the "A" array
0
 
LVL 1

Expert Comment

by:netwiz562
ID: 8448667
This question has been answered.  Please give someone the credit eokorie.

-NetWiz562
0
 

Author Comment

by:eokorie
ID: 8449309
This is worked a treat...sorry i took so long to get back
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question