Jump to content

how to chk multiple times in a text file


 Share

Recommended Posts

Hi. I have a text file like this

[APPLE]

Color: Red

Quantity: 100

Country: France

[Orange]

Color: Orange

Quantity: 300

Country: China

I want to use a loop to check for Apple and get the country. The outer loop is easy. I can use a while....wend to get apple. But how do I get the 2nd item.

Thanks.

Link to comment
Share on other sites

Convert it to a 2D array was my method... :P

#include <Array.au3>
Global $aOutPut[1][4]
;[n][0] = Fruit
;[n][1] = Colour
;[n][2] = Quantity
;[n][3] = Country
Global $iCount = 0
Global $ic = 0

$textfile_contents = "[APPLE]" & @CRLF
$textfile_contents &= "Color: Red" & @CRLF
$textfile_contents &= "Quantity: 100" & @CRLF
$textfile_contents &= "Country: France" & @CRLF & @CRLF
$textfile_contents &= "[Orange]" & @CRLF
$textfile_contents &= "Color: Orange" & @CRLF
$textfile_contents &= "Quantity: 300" & @CRLF
$textfile_contents &= "Country: China"

$aText = StringSplit ($textfile_contents, @CRLF, 1)

_ArrayDisplay ($aText)

For $i = 1 To $aText[0]
    If StringRegExp ($aText[$i], "(\[)(.*?)(\])") Then
        $iCount += 1
        $iC = 0
        ReDim $aOutPut[$iCount + 1][4]
        $aOutPut[$iCount][$iC] = StringTrimRight(StringTrimLeft ($aText[$i], 1),1)
        $ic += 1
    ElseIf $aText[$i] <> "" Then
        $aOutPut[$iCount][$iC] = StringTrimLeft ($aText[$i], StringInStr ($aText[$i], ": ", 1))
        $ic += 1
    EndIf
Next
$aOutPut[0][0] = $iCount

_ArrayDisplay ($aOutPut)
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...