Jump to content
Sign in to follow this  
NS5

Problem reading text file

Recommended Posts

NS5

Greetings my fellow coders! I have a problem thinking on converting this text file to INI file and just get the necessary "key" and "value". Here is an example.

TEXT FILE:

BUY SELL

ITEMNUM 1 [bEVERAGE] 0.05 0.09 Name1[1]Name2[3]

ITEMNUM 2 [bEVERAGE] 0.05 0.09 Name1[1]Name2[3]

ITEMNUM 3 [bEVERAGE] 0.06 0.09 Name1[1]Name2[3]

ITEMNUM 4 [bEVERAGE] 0.06 0.1 Name1[1]Name2[3]

ITEMNUM 5 [bEVERAGE] 0.06 0.1 Name1[1]Name2[3]

END of TEXT FILE:

~==========================================================================================================

Based on the text file, I want to get the rest of the text except for the Name1[1]Name2[3]. Now my INI file should

look like this shown below. Its like updating the prices from the text file to my INI.

~=================================================================================================

INI FILE:

[GROCERIES]

ITEMNUM 1 [bEVERAGE]= 0.090 0.018 0.045 0.060

ITEMNUM 2 [bEVERAGE]= 0.090 0.018 0.045 0.060

ITEMNUM 3 [bEVERAGE]= 0.100 0.200 0.050 0.075

ITEMNUM 4 [bEVERAGE]= 0.100 0.200 0.050 0.075

END OF INI FILE:

Make sure to rename Prices.txt to Prices.ini because forums does not allow INI to be uploaded. A little help on this to start with is gladly appreciated.

#include-once
$file = "prices.ini"

$bpk = 0.8

$spk = 1

$altlimit = 0.5

if FileExists( $file ) = 0 then Exit

InetGet("http://supernovabots.com/prices_0.txt", "prices_0.txt" )

$F = FileOpen( $file, 0 )

$buying = "buyingprices.ini"
$selling = "sellingprices.ini"

While 1
    $line = FileReadLine($F)

    If @error = -1 Then ExitLoop

    $cardname = StringLeft( $line, 47 )

    $price = round( StringMid( $line, 48, 9 ), 1 )

    if $price = 0 then ContinueLoop

    if $price > 0.2 then $price = Round( $price * $bpk, 1 )

    if $price < 0.01 then $price = 0.01

    $priceSELL = round( StringMid( $line, 58, 9 ) * $spk, 1 )

    If $priceSELL < $altlimit then $priceSELL = $altlimit

    $ptxt = IniRead( "prices.ini", "CARDS", $cardname, "999 999 0 0" )
        $p = StringSplit( $ptxt, " " )

    IniWrite( "prices.ini", "CARDS", $cardname, $price & " " & $p[2] & " " & $priceSELL & " " & $p[0] )


Wend

FileClose( $F )

PRICES.txt

Edited by NS5

Share this post


Link to post
Share on other sites
Bowmore

I'm in a generous mood this evening so here is one method of doing it.

#include <file.au3>
Global $aTextArray[1]
Global $aParts = 0

;Read each line of text file into an array
_FileReadToArray("c:\My Text File.txt",$aTextArray)
    ; Check if sucessful
    If IsArray($aTextArray) Then
        ; Process each line from text file
        For $i = 1 To $aTextArray[0] Step 1
            ; Use regular expression to get the 2 parts we are interested in
            $aParts = StringRegExp($aTextArray[$i], "^(.*?]) ([0-9.]* [0-9.]*)", 3)
            If IsArray($aParts) Then
                ; Write to ini file
                IniWrite("c:\My INI File.ini", "GROCERIES", $aParts[1], $aParts[2])
            EndIf
        Next
    EndIf

Exit

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.