[UDF] _Array2D.au3 - Gestion tableaux 2D

Partagez des fonctions et des UDF AutoIt.
Règles du forum
.
Répondre
Avatar du membre
ricky
Niveau 7
Niveau 7
Messages : 443
Enregistré le : ven. 06 févr. 2009 09:25
Localisation : Suisse
Status : Hors ligne

[UDF] _Array2D.au3 - Gestion tableaux 2D

#1

Message par ricky »

Hello,

voici un petit udf pour gérer les tableaux 2D :

Voici le code de _Array2D.au3

Code : Tout sélectionner

#include-Once

; #INDEX# =======================================================================================================================
; Title .........: _Array2D
; AutoIt Version : 3.3.6.1 (tested on this version)
; Language ......: English
; Description ...: Functions for manipulating array 2D
; Author(s) .....: Ricky with a part of code from array.au3
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;_ArrayAdd2D
;_ArrayConcatenate2D
;_Array2DToHtml
; ===============================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name...........: _ArrayConcatenate2D
; Version........: 1.1
; Last Modified..: 09.05.2011
; Description ...: Concatenate two arrays 2D.
; Syntax.........: _ArrayConcatenate(ByRef $Target, $Source, $Row0Update = 0)
; Parameters ....: $Target - The array 2D to concatenate onto
;                  $Source - The array 2D to concatenate from
;                  $Row0Update - 0 (default) do nothing
;                              - 1 update the number of lignes in $Target[0][0]
; Return values .: Success - $Target's new size
;                  Failure - 0, sets @error to:
;                  |1 - $Target is not an array
;                  |2 - $Source is not an array
;                  |3 - $Target is not a 2 dimensional array
;                  |4 - $Source is not a 2 dimensional array
;                  |5 - $Target and $Source is not a 2 dimensional array
;                  |6 - The cols of the source aren't the same as the target
;                  |7 - $Target[0][0] is not a number
; Author ........: Ricky
; Modified.......: Partypooper - added target start index
; Remarks .......: Based on _ArrayConcatenate from Ultima
; Related .......:
; Link ..........: http://www.autoitscript.fr/forum/viewtopic.php?f=21&t=7413
; Example .......: Yes
; ===============================================================================================================================

Func _ArrayConcatenate2D(Byref $Target, $Source, $Row0Update = 0)

    If Not IsArray($Target) Then Return SetError(1, 0, 0)
    If Not IsArray($Source) Then Return SetError(2, 0, 0)
    If UBound($Target, 0) <> 2 Then
        If UBound($Source, 0) <> 2 Then Return SetError(5, 0, 0)
        Return SetError(3, 0, 0)
    EndIf
    If UBound($Source, 0) <> 2 Then Return SetError(4, 0, 0)

    Local $rowsTarget = UBound($Target)
    Local $colsTarget = UBound($Target, 2)
    Local $rowsSource = UBound($Source)
    Local $colsSource = UBound($Source, 2)

    if $colsTarget <> $colsSource then Return SetError(6, 0, 0)

    $newRowsSize = $rowsTarget+$rowsSource
    if $Row0Update = 1 then
        If NOT IsNumber($Target[0][0]) Then Return SetError(7, 0, 0)
        $Target[0][0] = $newRowsSize - 1
    EndIf
    Redim $Target[$newRowsSize][$colsTarget]

    for $i=$rowsTarget to $newRowsSize - 1

        for $j = 0 to $colsTarget - 1
            $Target[$i][$j] = $Source[$i-$rowsSource][$j]
        Next
    Next

    Return $newRowsSize

EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _ArrayAdd2D
; Version........: 1.1
; Last Modified..: 09.05.2011
; Description ...: Adds an array 1D at the end of an existing array2D.
; Syntax.........: _ArrayAdd(ByRef $Target, $Source, $Row0Update = 0)
; Parameters ....: $Target - Array to modify
;                  $vValue  - Array to add
;                  $Row0Update - 0 (default) do nothing
;                              - 1 update the number of lignes in $Target[0][0]
; Return values .: Success - Index of last added item
;                  Failure - 0, sets @error
;                  |1 - $Target is not an array
;                  |2 - $Source is not an array
;                  |3 - $Target is not a 2 dimensional array
;                  |4 - $Source is not a 1 dimensional array
;                  |5 - The rows of the source aren't in the same size of the cols of the target
;                  |6 - $Target[0][0] is not a number
; Author ........: Ricky
; Modified.......:
; Remarks .......: Based on _ArrayAdd from Jos van der Zande <jdeb at autoitscript dot com>
; Related .......:
; Link ..........: http://www.autoitscript.fr/forum/viewtopic.php?f=21&t=7413
; Example .......: Yes
; ===============================================================================================================================

Func _ArrayAdd2D(ByRef $Target, $Source, $Row0Update = 0)
    If Not IsArray($Target) Then Return SetError(1, 0, 0)
    If Not IsArray($Source) Then Return SetError(2, 0, 0)
    If UBound($Target, 0) <> 2 Then Return SetError(3, 0, 0)
    If UBound($Source, 0) <> 1 Then Return SetError(4, 0, 0)

    Local $rowsSource = UBound($Source)
    Local $colsTarget = UBound($Target, 2)
    if $rowsSource <> $colsTarget Then Return SetError(5, 0, 0)

    Local $iUBound = UBound($Target)

    if $Row0Update = 1 then
        If NOT IsNumber($Target[0][0]) Then Return SetError(6, 0, 0)
        $Target[0][0] = $iUBound
    EndIf

    ReDim $Target[$iUBound + 1][$colsTarget]

    For $i=0 to $colsTarget - 1
        $Target[$iUBound][$i] = $Source[$i]
    Next
    Return $iUBound
EndFunc   ;==>_ArrayAdd

; #FUNCTION# ====================================================================================================================
; Name...........: _Array2DToHtml
; Version........: 1.1
; Last Modified..: 03.05.2011
; Description ...: Write an 2D array in a html FILE
; Syntax.........: _Array2DToHtml($sArray, $FileName, $Title, $row0Names = 0)
; Parameters ....: $sArray    - Array with the values
;                  $FileName  - File name with the path and the extension (if a file exists, the oldest is deleted)
;                  $Title     - Title of the file
;                  $row0Names - 0 (default) the name of the cols aren't present
;                             - 1 the name of the cols are present in the row 0
; Return values .: Success - 1
;                  Failure - 0 if file not opened in writemode, file is read only, or file cannot otherwise be written to.
; Author ........: Ricky
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........: http://www.autoitscript.fr/forum/viewtopic.php?f=21&t=7413
; Example .......: Yes
; ===============================================================================================================================

Func _Array2DToHtml($sArray, $FileName, $Title, $row0Names = 0)

    $rows = UBound($sArray) - 1 ; nombre de lignes
    $cols = UBound($sArray, 2) - 1 ; nombre de colonnes

    $Output = ''


    $Output &= '<html>' & @CRLF & '<head>' & @CRLF
    $Output &= '<meta name="generator" content="AutoIT Array2DToHtml"/>' & @CRLF
    $Output &= '<meta name="author" content="Ricky"/>' & @CRLF
    $Output &= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>' & @CRLF & '<style type="text/css">' & @CRLF
    $Output &= 'table {border: solid #808080; border-top-width:1; border-left-width:0; border-bottom-width:0; border-right-width:1;}' & @CRLF
    if $row0Names = 1 then $Output &= 'th {background-color: #0080C0; color: #FFFFFF; border: solid #808080; border-top-width:0; border-left-width:1; border-bottom-width:1; border-right-width:0;}' & @CRLF
    $Output &= 'td {background-color: #FFFFFF; color: #000000; border: solid #808080; border-top-width:0; border-left-width:1; border-bottom-width:1; border-right-width:0;}' & @CRLF
    $Output &= 'td.space {background-color: #FFFFFF; color: #000000; border: solid #808080; border-top-width:0; border-left-width:0; border-bottom-width:1; border-right-width:0;}' & @CRLF
    $Output &= '</style>' & @CRLF & '</head>' & @CRLF & '<body>' & @CRLF & '<center>' & @CRLF & '<h2>' & $Title & '</h2>' & @CRLF
    $Output &= '<table border="0" cellspacing="0" cellpadding="2" width="100%">' & @CRLF

    for $i=1 to $rows

    $Output &= '<tr>' & @CRLF


        if $i = 1 and $row0Names = 1 then
            ; écriture de la ligne de titre qui est différente
            for $j=0 to $cols
                $Output &= '<th align="center"><font size="1" face="arial">&nbsp;' & $sArray[0][$j] & '</font></th>' & @CRLF
            Next
            $Output &= '</tr>' & @CRLF & '<tr>' & @CRLF
        EndIf

        for $j=0 to $cols
                $Output &= '<td align="left"><font size="1" face="arial">&nbsp;' & $sArray[$i][$j] & '</font></td>' & @CRLF
        Next

    $Output &= '</tr>' & @CRLF
    next

    $Output &= '</table>' & @CRLF & '</center>' & @CRLF & '</body>' & @CRLF & '</html>'
    if FileExists($FileName) then FileDelete($FileName)
    $Result = FileWrite($FileName, $Output )
    Return $Result
EndFunc
 
Script d'exemple :

Code : Tout sélectionner

#include <Array.au3>
#include "_Array2D.au3"

Local $avArrayTarget[5][2]
$avArrayTarget[0][0]=4
$avArrayTarget[1][0]="b"
$avArrayTarget[2][0]="c"
$avArrayTarget[3][0]="d"
$avArrayTarget[4][0]="e"
$avArrayTarget[0][1]="f"
$avArrayTarget[1][1]="g"
$avArrayTarget[2][1]="h"
$avArrayTarget[3][1]="i"
$avArrayTarget[4][1]="j"
Local $avArraySource[5][2]
$avArraySource[0][0]=1
$avArraySource[1][0]=2
$avArraySource[2][0]=3
$avArraySource[3][0]=4
$avArraySource[4][0]=5
$avArraySource[0][1]=6
$avArraySource[1][1]=7
$avArraySource[2][1]=8
$avArraySource[3][1]=9
$avArraySource[4][1]=10

Local $Array1D[2] = ["Ajout","ligne"]

_ArrayDisplay($avArrayTarget, "$avArrayTarget BEFORE _ArrayConcatenate2D()")
if _ArrayConcatenate2D($avArrayTarget, $avArraySource,1) = 0 Then MsgBox(0,"Error", "Message : " & @error)
_ArrayDisplay($avArrayTarget, "$avArrayTarget AFTER _ArrayConcatenate2D()")
if _ArrayAdd2D($avArrayTarget,$Array1D,1) = 0 Then MsgBox(0,"Error", "Message : " & @error)
_ArrayDisplay($avArrayTarget, "$avArrayTarget AFTER _ArrayAdd2D()")

Local $avArray[15][3] = [ _
["Name", "Value", "int"], _
["A", "d", 1], _
["b", "g", 4], _
["z", "f", 12], _
["A", "c", 3], _
["b", "e", 6], _
["z", "e", 13], _
["", "z", 7], _
["z", "a", 14], _
["A", "B", 2], _
["", "t", 8], _
["", "y", 9], _
["b", "f", 5], _
["y", "f", 11], _
["", "A", 10]]
$FileName =  @ScriptDir & "\test.html"
$Title = "Test of the function"

Array2DToHtml($avArray, $FileName, $Title, 1)
Si vous avez des remarques ou améliorations à y apporter, faites moi signe.
Modifié en dernier par ricky le lun. 09 mai 2011 12:16, modifié 3 fois.
Avatar du membre
Tlem
Site Admin
Site Admin
Messages : 11773
Enregistré le : ven. 20 juil. 2007 21:00
Localisation : Bordeaux
Status : Hors ligne

Re: [UDF] _Array2D.au3 - Gestion tableaux 2D

#2

Message par Tlem »

Pas mal. Ceci dit, les tableaux doivent avoir impérativement la même dimension ...

Cela fait au moins 10 mois que je voulais écrire quelques fonctions de gestion de tableau 2D, je n'ai plus qu'à m'y remettre. ;)
J'essaie de présenter ça rapidement (sans attendre 10 mois).
J'ai déjà une ébauche, mais je voulais peaufiner la bête. :mrgreen:
Thierry

Rechercher sur le forum ----- Les règles du forum
Le "ça ne marche pas" est une conséquence commune découlant de beaucoup trop de raisons potentielles ...

Une idée ne peut pas appartenir à quelqu'un. (Albert Jacquard) tiré du documentaire "Copié n'est pas volé".
Avatar du membre
ricky
Niveau 7
Niveau 7
Messages : 443
Enregistré le : ven. 06 févr. 2009 09:25
Localisation : Suisse
Status : Hors ligne

Re: [UDF] _Array2D.au3 - Gestion tableaux 2D

#3

Message par ricky »

Tlem a écrit :Pas mal. Ceci dit, les tableaux doivent avoir impérativement la même dimension ...
Merci. Oui, d'ou mes tests. Mais pour le _ArrayAdd2D, on entre un 1d pour le mettre dans un 2d!
Tlem a écrit :Cela fait au moins 10 mois que je voulais écrire quelques fonctions de gestion de tableau 2D, je n'ai plus qu'à m'y remettre. ;)
J'essaie de présenter ça rapidement (sans attendre 10 mois).
J'ai déjà une ébauche, mais je voulais peaufiner la bête. :mrgreen:
Quelles genre de fonctions? Tu veux que j'en fasse une ou deux, ou trois 8) ?
Avatar du membre
Tlem
Site Admin
Site Admin
Messages : 11773
Enregistré le : ven. 20 juil. 2007 21:00
Localisation : Bordeaux
Status : Hors ligne

Re: [UDF] _Array2D.au3 - Gestion tableaux 2D

#4

Message par Tlem »

Surprise !!! 8)
Thierry

Rechercher sur le forum ----- Les règles du forum
Le "ça ne marche pas" est une conséquence commune découlant de beaucoup trop de raisons potentielles ...

Une idée ne peut pas appartenir à quelqu'un. (Albert Jacquard) tiré du documentaire "Copié n'est pas volé".
Avatar du membre
ricky
Niveau 7
Niveau 7
Messages : 443
Enregistré le : ven. 06 févr. 2009 09:25
Localisation : Suisse
Status : Hors ligne

Re: [UDF] _Array2D.au3 - Gestion tableaux 2D

#5

Message par ricky »

Zut, 10 mois à attendre :lol:

Image
Avatar du membre
jchd
AutoIt MVPs (MVP)
AutoIt MVPs (MVP)
Messages : 2272
Enregistré le : lun. 30 mars 2009 22:57
Localisation : Sud-Ouest de la France (43.622788,-1.260864)
Status : Hors ligne

Re: [UDF] _Array2D.au3 - Gestion tableaux 2D

#6

Message par jchd »

Pour patienter, voici un module sans prétention permettant d'extraire des parties d'un tableau d'entrée vers une variable simple, une liste, un sous-tableau 2-, 3- ou 4-D selon les cas. Il y a aussi une fonction de transposition (2- à 4-D).

Désolé pour les commentaires en anglais. Le format est "brut de fonderie" car je n'avais pas trop le temps d'en faire une UDF propre et complète lorsque j'avais eu besoin de ces fonctions. Je ne garantis pas non plus qu'il n'y ait pas un bon gros nid de bogues caché dedans :twisted:
Si ça peut servir à quelqu'un, tant mieux.

EDIT : on peut réaliser une fonction d'extraction pour toute dimension (jusqu'au maxi de 64 autorisé par AutoIt) mais la réalisation serait alors bien trop lourde et ruineuse en cycles. Ca rame déjà pas mal ainsi !
Fichiers joints
ArrayParts.au3
(26.49 Kio) Téléchargé 362 fois
La cryptographie d'aujourd'hui c'est le taquin plus l'électricité.
Avatar du membre
ricky
Niveau 7
Niveau 7
Messages : 443
Enregistré le : ven. 06 févr. 2009 09:25
Localisation : Suisse
Status : Hors ligne

Re: [UDF] _Array2D.au3 - Gestion tableaux 2D

#7

Message par ricky »

Correction de petits bugs, voir 1er topic
Avatar du membre
Tlem
Site Admin
Site Admin
Messages : 11773
Enregistré le : ven. 20 juil. 2007 21:00
Localisation : Bordeaux
Status : Hors ligne

Re: [UDF] _Array2D.au3 - Gestion tableaux 2D

#8

Message par Tlem »

Oups, 6 mois après, je me rend compte que je n'ai pas donné le lien vers ces fameuses fonctions. :oops:

Vous trouverez l'UDF ArrayEx.au3 sur ce lien : http://www.autoitscript.fr/forum/viewto ... =21&t=7582
Thierry

Rechercher sur le forum ----- Les règles du forum
Le "ça ne marche pas" est une conséquence commune découlant de beaucoup trop de raisons potentielles ...

Une idée ne peut pas appartenir à quelqu'un. (Albert Jacquard) tiré du documentaire "Copié n'est pas volé".
Répondre