Jump to content

Create three arrays from one array


Arik
 Share

Go to solution Solved by ripdad,

Recommended Posts

Hi guys,

Although this is my first post, I'm not new here and have used your great scripts to automate things I needed. I thank you all for helping me accomplish what I needed.

By using the help file & your scripts I created a simple script of a GUI with a dropdown menu to run installers of software.

Here is a reduced version of the script:

GUICreate("Setup Menu", 175, 40)

$InstallComboBox = GUICtrlCreateCombo("", 10, 10, 100, 20)

$Displays = IniReadSection("Setup.ini", "Displays")
Local $DisplayName[$Displays[0][0]]
For $i = 1 To $Displays[0][0]
$DisplayName[$i-1] = $Displays[$i][1]
Next

$Folders = IniReadSection("Setup.ini", "Folders")
Local $FolderName[$Folders[0][0]]
For $i = 1 To $Folders[0][0]
$FolderName[$i-1] = $Folders[$i][1]
Next

$Launchers = IniReadSection("Setup.ini", "Launchers")
Local $LauncherName[$Launchers[0][0]]
For $i = 1 To $Launchers[0][0]
$LauncherName[$i-1] = $Launchers[$i][1]
Next

$InstallEntries = ""

For $i = 0 To $Displays[0][0]-1 Step 1
$InstallEntries &= "|" & $DisplayName[$i]
Next

GUICtrlSetData(-1, $InstallEntries)

$InstallButton = GUICtrlCreateButton("Install", 117, 10, 50, 20)

GUISetState(@SW_SHOW)

While 1
$msg = GUIGetMsg()
Select
Case $msg = $InstallButton
For $i = 0 To $Folders[0][0]-1 Step 1
Select
Case GUICtrlRead($InstallComboBox) = $DisplayName[$i]
ShellExecute($FolderName[$i] & "\" & $LauncherName[$i])
EndSelect
Next

Case $msg = -3
ExitLoop
EndSelect
WEnd
Here is the ini file that it refers to:

[Displays]
Name1=Adobe Reader
Name2=Flash Player
Name3=Mozilla Firefox
Name4=Nero Express

[Folders]
Folder1=Reader
Folder2=Flash
Folder3=Firefox
Folder4=Nero

[Launchers]
Launcher1=AcroRead.msi
Launcher2=install_flash_player_11_plugin.msi
Launcher3=Firefox-16.0.2-en-US.msi
Launcher4=setup.exe
What I want to do is place all the entries of the three sections together in one section and still create the three arrays. The ini file will look like this:

[Entries]
Name1=Adobe Reader
Folder1=Reader
Launcher1=AcroRead.msi
Name2=Flash Player
Folder2=Flash
Launcher2=install_flash_player_11_plugin.msi
Name3=Mozilla Firefox
Folder3=Firefox
Launcher3=Firefox-16.0.2-en-US.msi
Name4=Nero Express
Folder4=Nero
Launcher4=setup.exe
It's much more convenient for me with this format when it comes to hundreds of entries. Edited by Arikm2005
Link to comment
Share on other sites

And your question is...?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Maybe you can do it with 2 arrays...

#include 'Array.au3'

Opt('MustDeclareVars', 1)

Local $sIniFile = @ScriptDir & '\Setup.ini'
Local $aSection, $aSections = IniReadSectionNames($sIniFile)

_ArrayDisplay($aSections, 'Software Sections')

For $i = 1 To $aSections[0]
    $aSection = IniReadSection($sIniFile, $aSections[$i])
    ; $aSection[1][1] = Display
    ; $aSection[2][1] = Folder
    ; $aSection[3][1] = Launcher
    MsgBox(0, $aSection[1][1], @ScriptDir & '\' & $aSection[2][1] & '\' & $aSection[3][1])
Next

 

[Reader]
Display=Adobe Reader
Folder=Reader
Launcher=AcroRead.msi

[Player]
Display=Flash Player
Folder=Flash
Launcher=install_flash_player_11_plugin.msi

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

The IniReadSection command reads all of the entries in an ini section. With the new ini file I can't tell it to read entries 1,4,7,10 to array #1, the entries 2,5,8,11 to array #2 and the entries 3,6,9,12 to array #3. So my only option is to read all of the entries to one big array and somehow to create three arrays from the big one (jumps of 3 between one entry to the next). How should the code be written in order to do that?

Link to comment
Share on other sites

Because the INI file entries can be written in any order you can't logically get it to read it back in a logical fashion using IniReadSection unless you create the ini file manually. What's wrong with just doing it correctly and using individual sections for each program? Doing it the way you want pretty much defeats the whole purpose of using an INI file, you might as well use a flat text file and read it line by line.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

BrewManNH, you're right. If I will have only one section, then it's not different from a flat text file. Just use another function to create the arrays. Now that I think of it, updating a flat text file with new lines is not convenient as I thought. Using individual sections for each program like ripdad's reply is better.

ripdad, thanks for your help, but can you help a newbie & reply with the whole corrected script (including the GUI section)?

Also, if it makes any difference to the script, The section names will always be as the display names.

The new INI file is:

[Adobe Reader]
Display=Adobe Reader
Folder=Reader
Launcher=AcroRead.msi

[Flash Player]
Display=Flash Player
Folder=Flash
Launcher=install_flash_player_11_plugin.msi

[Mozilla Firefox]
Display=Mozilla Firefox
Folder=Firefox
Launcher=Firefox-16.0.2-en-US.msi

[Nero Express]
Display=Nero Express
Folder=Nero
Launcher=setup.exe
Edited by Arik
Link to comment
Share on other sites

 

BrewManNH, you're right. If I will have only one section, then it's not different from a flat text file. Just use another function to create the arrays. Now that I think of it, updating a flat text file with new lines is not convenient as I thought. Using individual sections for each program like ripdad's reply is better.

ripdad, thanks for your help, but can you help a newbie & reply with the whole corrected script (including the GUI section)?

Also, if it makes any difference to the script, The section names will always be as the display names.

The new INI file is:

[Adobe Reader]
Display=Adobe Reader
Folder=Reader
Launcher=AcroRead.msi

[Flash Player]
Display=Flash Player
Folder=Flash
Launcher=install_flash_player_11_plugin.msi

[Mozilla Firefox]
Display=Mozilla Firefox
Folder=Firefox
Launcher=Firefox-16.0.2-en-US.msi

[Nero Express]
Display=Nero Express
Folder=Nero
Launcher=setup.exe

 

I modified your script to be easier for me.

See if this is what you want:

#include <WindowsConstants.au3>
#include <GuiComboBox.au3>
#include <Array.au3>

Local $IniFile = @ScriptDir & "\Setup.ini"
Local $InstallEntries = ""
Local $secNames = IniReadSectionNames($IniFile)

Local $Gui = GUICreate("Setup Menu", 280, 100)
Local $InstallButton = GUICtrlCreateButton("Install", 210, 10, 50, 25)
Local $InstallEvol = GUICtrlCreateLabel("", 20, 50, 240, 20)
Local $InstallComboBox = GUICtrlCreateCombo("", 20, 10, 170, 25)
;Local $InstallComboBox = GUICtrlCreateCombo("", 10, 10, 100, 20, $CBS_DROPDOWN + $CBS_AUTOHSCROLL + $WS_VSCROLL + $CBS_SORT); Sort

For $i = 1 To UBound($secNames) - 1
    If $InstallEntries = "" Then
        $InstallEntries &= $secNames[$i]
    Else
        $InstallEntries &= "|" & $secNames[$i]
    EndIf
Next
GUICtrlSetData($InstallComboBox, $InstallEntries)


GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $InstallButton
            Install()
        Case $msg = $InstallComboBox
            GUICtrlSetData($InstallEvol, "")
        Case $msg = -3
            ExitLoop
    EndSelect
WEnd


Func Install()
    Local $sec = GUICtrlRead($InstallComboBox)
    If $sec <> "" Then
        Local $aSec = IniReadSection($IniFile, $sec)
        ;_ArrayDisplay($aSec)
        Local $DisplayName = $aSec[1][1]
        Local $FolderName = $aSec[2][1]
        Local $LauncherName = $aSec[3][1]
        GUICtrlSetData($InstallEvol, "Installing: " & $DisplayName)
        ;ShellExecuteWait($FolderName & "\" & $LauncherName)
        MsgBox(0, "", $DisplayName & " # " & $FolderName & " # " & $LauncherName)
        GUICtrlSetData($InstallEvol, "Installing " & $DisplayName & " is complete!")
    Else
        MsgBox(16, "Error", "You have to choose an application to install!")
    EndIf
EndFunc   ;==>Install
Edited by JCEF
Link to comment
Share on other sites

  • Solution

#include 'Array.au3'

Opt('MustDeclareVars', 1)

Local $sIniFile = @ScriptDir & '\Setup.ini'
If Not FileExists($sIniFile) Then
    MsgBox(0, 'Error', 'IniFile Not Found')
    Exit
EndIf

Local $sCombo, $sFile, $InstallEntries = ''
Local $aSection, $aSections = IniReadSectionNames($sIniFile)

Local $GUI = GUICreate('Setup Menu', 175, 40)
Local $InstallComboBox = GUICtrlCreateCombo('', 10, 10, 100, 20)
Local $InstallButton = GUICtrlCreateButton('Install', 117, 10, 50, 20)

For $i = 1 To $aSections[0]; <-- section names, same as display names
    $InstallEntries &= '|' & $aSections[$i]
Next
GUICtrlSetData($InstallComboBox, $InstallEntries)

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case -3
            GUIDelete($GUI)
            ExitLoop
        Case $InstallButton
            $sCombo = GUICtrlRead($InstallComboBox)
            For $i = 1 To $aSections[0]
                If $aSections[$i] = $sCombo Then
                    $aSection = IniReadSection($sIniFile, $aSections[$i])
                    If $aSection[0][0] = 2 Then; <-- check if good array.
                        ; $aSection[1][1] = Folder
                        ; $aSection[2][1] = Launcher
                        $sFile = @ScriptDir & '\' & $aSection[1][1] & '\' & $aSection[2][1]
                        If FileExists($sFile) Then
                            ShellExecute($sFile)
                        Else
                            MsgBox(0, 'File Not Found', $sFile)
                        EndIf
                    EndIf
                EndIf
            Next
    EndSwitch
WEnd
[Adobe Reader]
Folder=Reader
Launcher=AcroRead.msi

[Flash Player]
Folder=Flash
Launcher=install_flash_player_11_plugin.msi

[Mozilla Firefox]
Folder=Firefox
Launcher=Firefox-16.0.2-en-US.msi

[Nero Express]
Folder=Nero
Launcher=setup.exe

Nice refresher. Been awhile since I played with INI functions.

-edit- sp*

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
Share on other sites

ripdad & JCEF, both of the scripts work great with their corresponding INI files.

I'll use ripdad's script, which enables me to enter fewer lines of data to the ini file. I modified it with a few things JCEF wrote, which I would have considered later: the changing label below the combobox and the sorting of the combobox alphabetically. Now, when I update the INI file, I don't have to enter new sections according to the alphabet.

I thank you for your help. You're great guys!!!

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...