Jump to content
Sign in to follow this  
FaT3oYCG

[Solved] Reading in a formatted file.

Recommended Posts

FaT3oYCG

ok so i have a file that has a list of things that are under a higherarchy and need to be sorted into alphabetical order.

the format of each item is either -, --, or --- then 3 or four parts seperated by a . and then on some but not all a () at the end.

Example list:

-a.a

--a.a.a()

--a.a.b()

--a.a.c()

--a.a.d

---a.a.d.a()

---a.a.d.b()

-a.b

--a.b.a()

--a.b.b()

--a.b.c()

--a.b.d

---a.b.d.a()

---a.b.d.b()

etc. for about 17 different sections

the problem that i also have is that they need to be put into an array so that each one is held in a variable so that it can be used to populate a menu with the data and for further referencing the item throughout the program.

i have tried code with array add etc but i just cant seem to get it to work and i dont think the way i am doing it is the best anyway.

Dim $array[1][1][1]
  $filehandle = FileOpen(@ScriptDir & "\file.txt", 0)
  If $filehandle <> -1 Then
      $a = 1
      $b = 1
      $c = 1
      While @error <> -1
          If @error <> 1 Then
              $line = FileReadLine($filehandle)
              If StringLeft($line, 3) == "---" Then
                  _ArrayAdd($array[$a][$b][$c], StringRight($line, StringLen($line) - 3))
                  $c += 1
              ElseIf StringLeft($line, 2) == "--" Then
                  _ArrayAdd($array[$a][$b][$c], StringRight($line, StringLen($line) - 2))
                  $b += 1
              ElseIf StringLeft($line, 1) == "-" Then
                  _ArrayAdd($array[$a][$b][$c], StringRight($line, StringLen($line) - 1))
                  $a += 1
              Else
                  MsgBox(0, "Error", "Failed to read file, incorrect format")
              EndIf
          EndIf
      WEnd
  EndIf
 FileClose($filehandle)

any help appreciated.

Edited by FaT3oYCG

Interpreters have great power!Although they live in the shadow of compiled programming languages an interpreter can do anything that a compiled language can do, you just have to code it right.

Share this post


Link to post
Share on other sites
water

A hierarchy is best displayed in a TreeView. Here you can find a script to read a file like yours and display it in a TreeView.

The coding to populate the input file for the TreeView should be quite simple.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
FaT3oYCG

i need to put my items into a menu though and looking at that there isnt anything that would help me?


Interpreters have great power!Although they live in the shadow of compiled programming languages an interpreter can do anything that a compiled language can do, you just have to code it right.

Share this post


Link to post
Share on other sites
water

The basic problem is, that _ArrayAdd onyl supports 1 dimensional arrays. So you would have to expand the array yourself.

This can be done using ReDim $array. Another problem in your script is that when you increment $a you have to reset $b and $c to 1 and so on

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
FaT3oYCG

how would i display or read the contents of the array i seem to get an error every time i try.


Interpreters have great power!Although they live in the shadow of compiled programming languages an interpreter can do anything that a compiled language can do, you just have to code it right.

Share this post


Link to post
Share on other sites
water

I modified my previous posting.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
FaT3oYCG

after a lot more searching, solution:

#include <GUIConstants.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiMenu.au3>
#include <Array.au3>
#include <File.au3>

Dim $pgeLuaFunctionsArray[1]
_FileReadToArray(@ScriptDir & "\PGE Lua Function List.txt", $pgeLuaFunctionsArray)

$pgeMenu = GUICtrlCreateMenu("PGE Functions")

$level_1 = ""
$level_2 = ""
$level_3 = ""

For $i = 1 To UBound($pgeLuaFunctionsArray) - 1
    If StringLeft($pgeLuaFunctionsArray[$i], 3) = "---" Then
        If StringRight($pgeLuaFunctionsArray[$i], 2) = "()" Then
            $pgeLuaFunctionsArray[$i] = GUICtrlCreateMenuItem($pgeLuaFunctionsArray[$i], $level_2)
        Else
            $pgeLuaFunctionsArray[$i] = GUICtrlCreateMenu($pgeLuaFunctionsArray[$i], $level_2)
        EndIf
        $level_3 = $pgeLuaFunctionsArray[$i]
    ElseIf StringLeft($pgeLuaFunctionsArray[$i], 2) = "--" Then
        If StringRight($pgeLuaFunctionsArray[$i], 2) = "()" Then
            $pgeLuaFunctionsArray[$i] = GUICtrlCreateMenuItem($pgeLuaFunctionsArray[$i], $level_1)
        Else
            $pgeLuaFunctionsArray[$i] = GUICtrlCreateMenu($pgeLuaFunctionsArray[$i], $level_1)
        EndIf
        $level_2 = $pgeLuaFunctionsArray[$i]
        $level_3 = ""
    ElseIf StringLeft($pgeLuaFunctionsArray[$i], 1) = "-" Then
        If StringRight($pgeLuaFunctionsArray[$i], 2) = "()" Then
            $pgeLuaFunctionsArray[$i] = GUICtrlCreateMenuItem($pgeLuaFunctionsArray[$i], $pgeMenu)
        Else
            $pgeLuaFunctionsArray[$i] = GUICtrlCreateMenu($pgeLuaFunctionsArray[$i], $pgeMenu)
        EndIf
        $level_1 = $pgeLuaFunctionsArray[$i]
        $level_2 = ""
        $level_3 = ""
    Else
        MsgBox(0, "Error", "Failed to read function line: " & $i & ", incorrect format")
    EndIf
Next

$mainMenu = _GUICtrlMenu_GetMenu($frmMain)
$itemPGE = _GUICtrlMenu_GetItemSubMenu($mainMenu, 0)
For $i = 0 To 14
    $ItemPGESub = _GUICtrlMenu_GetItemSubMenu($itemPGE, $i)
    _GUICtrlMenu_SetMenuHeight($ItemPGESub, 220)
Next

GUISetState(@SW_SHOW)

While 1
    Sleep(100)
WEnd

cba changing the code it has all of the variable that i used if you cant understand then just change them fore something more relevant.

you can just run that code and it will work dont know if some of the headders are needed but they are in my main script.

enjoy.


Interpreters have great power!Although they live in the shadow of compiled programming languages an interpreter can do anything that a compiled language can do, you just have to code it right.

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  

×