Sign in to follow this  
Followers 0
BigJay517

Read a File

4 posts in this topic

I something like this has probably been posted a million times. I did quite a bit of searching and could not find another case that was closely enough related so that I could figure this out.

I have a text file that looks like this for example:

hello,hola
paper,el papel
use,usar

Now I want the program to read the file using the variable $word_eng to look for the first term.

$word_eng = "hello"

then i want it to find $word_eng and take the word after the comma "hola" and make it equal to $word_esp

so pretty much $word_eng = "hello" then $word_esp = "hola"

There is more to the program this is all I need help with though.

Anyone got any ideas?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Here Ya Go!

#include <file.au3>

; Each line of file is "English,Spanish"
Global $sFile = @ScriptDir & "\Dictionary.txt"
Global $avFile, $word_esp = "<not found>"

; Get Word to search for
$word_eng = InputBox("English", "Enter Word:")
If @error Then Exit

; Read file into an array
_FileReadToArray($sFile, $avFile)
If @error Then
    MsgBox(16, "Error", "Error opening file: " & $sFile)
    Exit
EndIf

; Search array for English and Spanish words
For $i = 1 To $avFile[0]
    If StringLeft($avFile[$i], StringLen($word_eng) + 1) = $word_eng & ","  Then
        $word_esp = StringStripWS(StringMid($avFile[$i], StringInStr($avFile[$i], ",") + 1), 8)
        ExitLoop
    EndIf
Next

; Display results
MsgBox(64, "Results", "English:  " & $word_eng & @CRLF & "Spanish:  " & $word_esp)

*Welcome to the forums :)

Edited by Hatcheda

Share this post


Link to post
Share on other sites

#include <File.au3>

Dim $myArray

If Not _FileReadToArray("somefile.txt", $myArray) Then
   MsgBox(4096,"Error", " Error reading file to Array     error:" & @error)
   Exit
EndIf

MsgBox(0,"",translate("hello"))

Func translate($word_eng)
    For $X = 1 to $myArray[0]
        $tempArray = StringSplit($myArray[$X],",")
        If $tempArray[1] = $word_eng Then Return $tempArray[2]
    Next
    Return SetError(1,1,"")
EndFunc

Share this post


Link to post
Share on other sites

Thank you and Thank you! :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0