Jump to content

Problem in strings..... again!


Recommended Posts

Ok.... i have a php file like this called configuration.php:

<?php

    $PUBLISHED_FOLDERS = array(
        "1" => array("name" => "Client1", "path" => "/client1"),
        "2" => array("name" => "Client2", "path" => "/client2"),
        "3" => array("name" => "Client3", "path" => "/client3"),
        "4" => array("name" => "Client4", "path" => "/client4"),
    );

    
    $USERS = array(
        "1" => array("name" => "Client1", "password" => "111111", "default_permission" => "rw", "folders" => array(
            "1" => NULL,
            "2" => NULL,
            "3" => NULL
        )),
        "1" => array("name" => "Client2", "password" => "222222", "default_permission" => "rw", "folders" => array(
            "1" => NULL,
            "2" => NULL,
            "3" => NULL
        )),
        "3" => array("name" => "Client3", "password" => "333333", "default_permission" => "rw", "folders" => array(
            "1" => NULL,
            "2" => NULL,
            "3" => NULL
        )),
        "4" => array("name" => "Clien4", "password" => "444444", "default_permission" => "ro", "folders" => array(
            "1" => "Custom name for Folder A",
            "2" => NULL
        ))
    );
    
?>

And this is my code:

#include <String.au3>
#include <array.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

local $trimclient, $trimpassword, $Combo1

Func ReadClients()
$clientlist = fileread("configuration.php");read the whole file to one long string
$clientlist = stringleft($clientlist,stringinstr($clientlist,"$USERS = array(") + stringlen("$USERS = array("));get rid of anything after $Users
$trimclient = _StringBetween($clientlist, '=> array("name" => "', '",')
EndFunc

ReadClients()

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 242, 178, 505, 295)
$Combo1 = GUICtrlCreateCombo("Clientes", 48, 64, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, _ArrayToString($trimclient))
$Button1 = GUICtrlCreateButton("Start", 50, 32, 97, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Func ReadClientsPasswords()
$passwordlist = fileread("configuration.php");read the whole file to one long string
$passwordlist = StringTrimLeft($passwordlist,stringinstr($passwordlist,"$USERS = array(") + stringlen("$USERS = array("));get rid of anything after $Users
$trimpassword = _StringBetween($passwordlist, GUICtrlRead($Combo1)&'", "password" => "', '",')
EndFunc

ReadClientsPasswords()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            IF GUICtrlRead($Combo1) = 'Clientes' Then
                msgBox(4160, "box",'Escolha um cliente')
            Else
                msgBox(4160, "box",GUICtrlRead($Combo1))
                msgBox(4160, "box",$trimpassword)
                EndIf
    EndSwitch
WEnd

I'm trying to display the password if i select a specific client.

For example... if i select Client1 I'd like a msgbox (or anything like that) displaying the specific password for that client. In this case 222222.

My logic with GUICtrlRead($Combo1)&'", "password" => "', '" seems right, but it doesn't work.

What am i doing wrong?

Link to comment
Share on other sites

  • Moderators

pintas,

I would use SREs to extract the client names and passwords into arrays and then use _ArraySearch to do the matching like this:

#include <GUIConstantsEx.au3>
#include <Array.au3>

Global $aClients, $aPasswords

ReadData()

$Form1 = GUICreate("Form1", 242, 178, 505, 295)

$Combo1 = GUICtrlCreateCombo("Clientes", 48, 64, 145, 25)
GUICtrlSetData(-1, _ArrayToString($aClients))
$Button1 = GUICtrlCreateButton("Start", 50, 32, 97, 25)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            If GUICtrlRead($Combo1) = 'Clientes' Then
                MsgBox(4160, "box", 'Escolha um cliente')
            Else
                MsgBox(4160, "box", GUICtrlRead($Combo1))
                ; Now search the client array for the value in the combo
                $iIndex = _ArraySearch($aClients, GUICtrlRead($Combo1))
                ; And display the corresponding element in the password array
                MsgBox(4160, "box", $aPasswords[$iIndex])

            EndIf
    EndSwitch
WEnd

Func ReadData()
    ; Read file
    $sFile = FileRead("configuration.php")
    ; Extract the client names
    $aClients = StringRegExp($sFile, "(?i)(?U)\x22name\x22 => \x22(.*)\x22, \x22password", 3)
    ; Extract the passwords
    $aPasswords = StringRegExp($sFile, "(?i)(?U)\x22password\x22 => \x22(.*)\x22, \x22default_permission", 3)
EndFunc   ;==>ReadData

With the flag parameter set to 3 the SRE looks for the values between the other parts of the pattern and returns them in an array - rather like a multiple _StringBetween (which actually uses an SRE to work!). :>

Please ask if you have any questions. :unsure:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Wow! It does work. :unsure:

In the help file the (?U) Invert greediness of quantifiers, i don't have a clue what that means. Could you please help me with this?

And the \x22, well i understand it's the ", i'm i right?

Link to comment
Share on other sites

  • Moderators

pintas,

Wow! It does work

What did you expect! ;)

Invert greediness of quantifiers

Normally SREs look for the longest match possible - (?U) makes them look for the shortest. This script should give you the idea:

#include <Array.au3>

$sString = "<>Tom<><>Dick<><>Harry<>"

; Longest match - looks for the first and last
$aArray = StringRegExp($sString, "<>(.*)<>", 3)
_ArrayDisplay($aArray)

; Invert and we look for the shortest match
$aArray = StringRegExp($sString, "(?U)<>(.*)<>", 3)
_ArrayDisplay($aArray)

the \x22, well i understand it's the "

Correct - much easier than trying to fiddle around with single and double quotes in the pattern. :>

All clear? :unsure:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

pintas,

Glad I could help. :unsure:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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