Jump to content
Ebola57

[SOLVED] Assign value in variable in array

Recommended Posts

Posted (edited)

Hi Community

That question may sounds stupid but I am not able to assign a value to a variable picked in a array

Why ??

Global $a = 1
Global $b = 2
Global $c = 3
Global $array [3] = [$a, $b, $c]
 
For $i = 0 To 2
    $array [$i] = $i + 10
Next

Msgbox(0,"", $a) ; -> 1

Regards

Edited by Ebola57

Share this post


Link to post
Share on other sites

You do not store a variable in the array but the VALUE of this variable.
In your loop you assign new values to the elements of the array. You do NOT modify the variables.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-30 - Version 1.6.2.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - 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
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites
Posted (edited)
12 minutes ago, water said:

You do not store a variable in the array but the VALUE of this variable.
In your loop you assign new values to the elements of the array. You do NOT modify the variables.

Okay !

So how to do a multiple assignment to a large bunch of variables  pls ?

Hav I to do with a two dimensionnal array only ? :( (much code to rewrite with such … )

Edited by Ebola57

Share this post


Link to post
Share on other sites

How many variables do we talk about?
In your above example a simple 1D array would be sufficient.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-30 - Version 1.6.2.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - 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
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites
Posted (edited)

Here's my function where  I'd like to rewrite the values

That values are the detected column number. Till now I used default values, but add a function that auto detect those

The purpose is : If > 70% of a column data match a regular expression, then I grabb and store it in the column number

(Function code is not completed)

Func _Excel_Find_by_Content ( $aExcel_Data, $Nb_of_Lines, $Nb_of_Columns ) ; Codage non terminé
    Local $sFunction_Name = "_Excel_Find_by_Content"
    Console ( "MSG", $sFunction_Name, '<<  Entrée dans "' & $sFunction_Name & '"' )
    Local $hTimer_Total  = TimerInit ( )
    Local $aExcel_Working = $aExcel_Data
    Local $aResult [8], $aRegExp [0]
    Local $iFound = 0, $iContent = 0, $iCol = 0, $iLin = 0, $iFound = 0, $Occur = 0, $Sum = 0
    Local $aFound_Header [13] [2], $aBlack_List [0]
    Local $aColumn [11] = [$iSub_category, $iLocation, $iOffice, $iLabel, $iAffected_To, $iManagerial_Unit, $iMail, $iMobile_1, $iPhone_1, $iMobile_2, $iPhone_2]   
    Local $aRegExp_Content [11] = ["(Hybride|Portable|Portable scientifique|Portable ultra-leger|Poste fixe|Poste scientifique)" _ ; Sous-catégorie de modèle
                                , "(\/[0-9]{2}\s\-\s[A-Z\-]{1,20}\/)" _ ; Emplacement
                                , "(?:(?:B|b)ureau|Plateau)\s" _ ; Bureau
                                , "(EGD[A-B]{1}[0-9]{6}|SC[A-Z]{1}[0-9]{7})" _ ; Etiquette
                                , "([A-Z]{1,30}\s[A-Z]{1,30}\s\-\s[A-Z]{1}[0-9]{5})" _ ; Affecté à
                                , "to_do_later" _ ; Unité Managériale
                                , "([a-z\-1-9]{1,30}\.[a-z\-]{1,30}\@(?:confidential)" _ ; Email
                                , "\'(\+[\d\s]+)" _ ; Mobile 1
                                , "\'(\+[\d\s]+)" _ ; Fixe 1
                                , "\'(\+[\d\s]+)" _ ; Mobile 2
                                , "\'(\+[\d\s]+)"] ; Fixe 2
    Local $aOccur [$Nb_of_Columns]

    For $iFound = 0 To 0
        Do
            For $iLin = 1 To $Nb_of_Lines -1
                $aRegExp = StringRegExp ( $aExcel_Working [$iLin] [$iCol], $aRegExp_Content [$iContent], 3 )
                If UBound ( $aRegExp ) > 0 Then 
                    $Occur += 1
                EndIf
            Next
            $aOccur [$iCol] = $Occur
            $Occur = 0
            $iCol += 1
        Until $iCol > $Nb_of_Columns - 1
        For $i = 0 To UBound ( $aOccur ) - 1
            $Sum += $aOccur [$i]
        Next
        If _ArrayMax ( $aOccur, 1 ) > Int ( $Sum * 70 / 100 ) Then
            $aColumn [$iFound] = _ArrayMaxIndex ( $aOccur, 1 ) ; Here's my problem
        EndIf
    Next
EndFunc

And my variables as delcared in a separate Variables.au3

Global $iLabel = 0              ; Index colonne Étiquette
Global $iLocation = 5           ; Index colonne Emplacement
Global $iOffice = 6             ; Index colonne Bureau
Global $iSub_category = 9       ; Index Sous-catégorie de modèle
Global $iAffected_To = 11       ; Index Affecté à
Global $iManagerial_Unit = 12   ; Index Unité Managériale
Global $iMail = 14              ; Index Mail
Global $iMobile_1 = 15          ; Index Mobile 1
Global $iPhone_1 = 16           ; Index Fixe 1
Global $iMobile_2 = 20          ; Index Mobile 2
Global $iPhone_2 = 21           ; Index Fixe 1

 

Edited by Ebola57

Share this post


Link to post
Share on other sites

Here one way :

Global $a = 1
Global $b = 2
Global $c = 3
Global $array [3] = ["a", "b", "c"]

For $i = 0 To 2
    Assign ($array[$i], $i + 10)
Next

Msgbox(0,"", $b)

 

Share this post


Link to post
Share on other sites

If I understand this function correctly you try check which column of the Excel array holds which kind of information (Location, Office, Phone 1 ...).
As multiple columns can hold the same information you count the number of occurrences in each column and then select the column with the highest number.

Means: You find phone numbers in column 2 five times and 7 times in column 15. So column 15 becomes the resulting column for the phone number.

Correct?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-30 - Version 1.6.2.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - 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
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites

You're absolutely right, the threshold is set to 70% of Matching

Tha $aBlack_List has for purpose to black list the column where it can be 2 kinds of data, I browse columns in a specific order to be sure of the result

Share this post


Link to post
Share on other sites
4 minutes ago, Nine said:

Here one way :

Global $a = 1
Global $b = 2
Global $c = 3
Global $array [3] = ["a", "b", "c"]

For $i = 0 To 2
    Assign ($array[$i], $i + 10)
Next

Msgbox(0,"", $b)

 

Right, but with that way I ahve to use Eval ( $array [$i] ) to get back the value right ?

What i wanted to get rid of is "array" that is not explicit at all. I have to manipulate those columns by tons in my code and would like Something explicit

Share this post


Link to post
Share on other sites
Posted (edited)

Why not use a global array rather than individual variables example:

Global $aIndex[11]
    $aIndex[0] = 0      ; Index colonne Étiquette
    $aIndex[1] = 5      ; Index colonne Emplacement
    $aIndex[2] = 6      ; Index colonne Bureau
    $aIndex[3] = 9      ; Index Sous-catégorie de modèle
    $aIndex[4] = 11     ; Index Affecté à
    $aIndex[5] = 12     ; Index Unité Managériale
    $aIndex[6] = 14     ; Index Mail
    $aIndex[7] = 15     ; Index Mobile 1
    $aIndex[8] = 16     ; Index Fixe 1
    $aIndex[9] = 20     ; Index Mobile 2
    $aIndex[10] = 21    ; Index Fixe 1

Then use:

$aIndex[$iFound] = _ArrayMaxIndex ( $aOccur, 1 )

I'm probably not understanding what you're trying to do.

Edited by Subz

Share this post


Link to post
Share on other sites
2 minutes ago, Ebola57 said:

Right, but with that way I ahve to use Eval ( $array [$i] ) to get back the value right ?

What is it used in the MsgBox  ?

Share this post


Link to post
Share on other sites
Just now, Subz said:

Why not use a global array rather than individual variables example:

Global $aIndex[11]
    $aIndex[0] = 0      ; Index colonne Étiquette
    $aIndex[1] = 5      ; Index colonne Emplacement
    $aIndex[2] = 6      ; Index colonne Bureau
    $aIndex[3] = 9      ; Index Sous-catégorie de modèle
    $aIndex[4] = 11     ; Index Affecté à
    $aIndex[5] = 12     ; Index Unité Managériale
    $aIndex[6] = 14     ; Index Mail
    $aIndex[7] = 15     ; Index Mobile 1
    $aIndex[8] = 16     ; Index Fixe 1
    $aIndex[9] = 20     ; Index Mobile 2
    $aIndex[10] = 21    ; Index Fixe 1

Then use:

$aIndex[$iFound] = _ArrayMaxIndex ( $aOccur, 1 )

 

Still same Reason, I'd like some explicit variable name through the code as i have to manipulate by tons and seeing "array" all the way complicate the thing

Share this post


Link to post
Share on other sites
Posted (edited)
2 minutes ago, Nine said:

What is it used in the MsgBox  ?

Mmmmhhh Indeed, sorry missed that
Indeed, that way fits really well my need :)
Did some try with Assign but did not understand th "$" simple way

Thanks !

Edited by Ebola57

Share this post


Link to post
Share on other sites

I suggest to use the approach of a global array as the main place to store the search results.
In any of your functions you can extract the needed value to a variable and use this variable for further processing.
If you need to alter the variable you need to alter the corresponding cell in the array as well.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-10-10 - Version 1.5.2.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-30 - Version 1.6.2.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - 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
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites
Posted (edited)
3 minutes ago, water said:

I suggest to use the approach of a global array as the main place to store the search results.
In any of your functions you can extract the needed value to a variable and use this variable for further processing.
If you need to alter the variable you need to alter the corresponding cell in the array as well.

This is what I did for other case
Think gonna rewrite this part, because if one modification is needed, it is so easy to add a column and manage it without full rewriting

Edited by Ebola57

Share this post


Link to post
Share on other sites

You could just use a function for example:

_UpdateVar($iFound, _ArrayMaxIndex ( $aOccur, 1 ))

Func _UpdateVar($_iIndex, $_iValue)
    Switch $_iIndex
        Case 0
            $iLabel = $_iValue
        Case 1
            $iLocation = $_iValue
        Case 2
            $iOffice = $_iValue
        Case 3
            $iSub_category = $_iValue
        Case 4
            $iAffected_To = $_iValue
        Case 5
            $iManagerial_Unit = $_iValue
        Case 6
            $iMail = $_iValue
        Case 7
            $iMobile_1 = $_iValue
        Case 8
            $iPhone_1 = $_iValue
        Case 9
            $iMobile_2 = $_iValue
        Case 10
            $iPhone_2 = $_iValue
    EndSwitch
EndFunc

 

Share this post


Link to post
Share on other sites
1 minute ago, Subz said:

You could just use a function for example:

_UpdateVar($iFound, _ArrayMaxIndex ( $aOccur, 1 ))

Func _UpdateVar($_iIndex, $_iValue)
    Switch $_iIndex
        Case 0
            $iLabel = $_iValue
        Case 1
            $iLocation = $_iValue
        Case 2
            $iOffice = $_iValue
        Case 3
            $iSub_category = $_iValue
        Case 4
            $iAffected_To = $_iValue
        Case 5
            $iManagerial_Unit = $_iValue
        Case 6
            $iMail = $_iValue
        Case 7
            $iMobile_1 = $_iValue
        Case 8
            $iPhone_1 = $_iValue
        Case 9
            $iMobile_2 = $_iValue
        Case 10
            $iPhone_2 = $_iValue
    EndSwitch
EndFunc

 

Indeed, but this is heavy as hell in code lines

Really thought we can manage easilly variable Inside array, but not …

Going to the way of a 2 dimensionnal array with explicit label

thanks to all for your replies :) !

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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...