Jump to content

Get Array From Array (opposite _ArrayDelete)


Go to solution Solved by mikell,

Recommended Posts

Hello

I need Get Array From Array

_ArrayDelete working fine on this code

#include <array.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen(_Excel_Open(), @ScriptDir&"\E.xlsx")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error opening E.xlsx")

        Local $aExcel = _Excel_RangeRead($oWorkbook, 1)
        $1 = $aExcel

        $Find = _ArrayFindAll($aExcel, "2", Default, Default, Default, Default,5)
        _ArrayInsert($Find, 0, UBound($Find))
        _ArrayDelete($aExcel, $Find)
        $2 = $aExcel

        _ArrayDisplay($1)
        _ArrayDisplay($2)

But i need get All Rows containing the value 2 on Column 5

difference between two arrays

Tnx.

E.xlsx

Edited by ahmeddzcom
Link to comment
Share on other sites

Try  this if ok

#include <array.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen(_Excel_Open(), @ScriptDir&"\E.xlsx")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error opening E.xlsx")

        Local $aExcel = _Excel_RangeRead($oWorkbook, 1)

        _ArrayDisplay($aExcel)

Local $aResult = _ArrayFindAll($aExcel, 2, Default, Default, Default, Default, 5)
_ArrayDisplay($aResult, "Found in Column 5")

 

I know that I know nothing

Link to comment
Share on other sites

and what it gives you this?

#include <array.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen(_Excel_Open(), @ScriptDir&"\E.xlsx")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error opening E.xlsx")

        Local $aExcel = _Excel_RangeRead($oWorkbook, 1)

;~         _ArrayDisplay($aExcel)

Local $aResult = _ArrayFindAll($aExcel, 2, Default, Default, Default, Default, 5)
_ArrayDisplay($aResult, "Found in Column 5")

 

I know that I know nothing

Link to comment
Share on other sites

or in comparison

#include <array.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen(_Excel_Open(), @ScriptDir & "\E.xlsx")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error opening E.xlsx")

Local $aExcel = _Excel_RangeRead($oWorkbook, 1)
$1 = $aExcel

$Find = _ArrayFindAll($aExcel, "2", Default, Default, Default, Default, 5)
_ArrayInsert($Find, 0, UBound($Find))
_ArrayDelete($aExcel, $Find)
$2 = $aExcel

;~         _ArrayDisplay($1, "$1")
;~         _ArrayDisplay($2, "$2")

For $x = 0 To $1[UBound($1) - 1][0]
    ConsoleWrite($1[$x][0] & "=" & _GetID($1[$x][0]) & @CRLF)
Next

Func _GetID($RowID)
    Local $ID
    For $i = 0 To $1[UBound($2) - 1][0]
        $ID = 0
        If $RowID = $2[$i][0] Then
            $ID = $2[$i][0]
            ExitLoop
        EndIf
    Next
    Return $ID
EndFunc   ;==>_GetID

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

I just noticed that $i = 0 is wrong because $RowID starts with 0.

So it's better this way

#include <array.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen(_Excel_Open(), @ScriptDir & "\E.xlsx")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error opening E.xlsx")

Local $aExcel = _Excel_RangeRead($oWorkbook, 1)
$1 = $aExcel

$Find = _ArrayFindAll($aExcel, "2", Default, Default, Default, Default, 5)
_ArrayInsert($Find, 0, UBound($Find))
_ArrayDelete($aExcel, $Find)
$2 = $aExcel

;~         _ArrayDisplay($1, "$1")
;~         _ArrayDisplay($2, "$2")

For $x = 0 To $1[UBound($1) - 1][0]
    ConsoleWrite($1[$x][0] & "=" & _GetID($1[$x][0]) & @CRLF)
Next

Func _GetID($RowID)
    Local $ID
    For $i = 0 To $1[UBound($2) - 1][0]
        $ID = "REMOVED"
        If $RowID = $2[$i][0] Then
            $ID = $2[$i][0]
            ExitLoop
        EndIf
    Next
    Return $ID
EndFunc   ;==>_GetID

 

I know that I know nothing

Link to comment
Share on other sites

I think OP would like to easily extract some non-contiguous full rows from an array, when the row indices are indicated in an external 1D array. For example something like this, using... a non-existant function :

Local $aExtract = _ArrayExtractEx($aExcel, $aIndices)

IIRC Melba23 added this functionality to _ArrayDelete (and _ArrayInsert too ?), I mean the possibility of using a range of indices in a 1D array. By the way, OP used this functionality when he scripted :

_ArrayDelete($aExcel, $Find)

$Find being a 1D array of indices, returned by _ArrayFindAll

Of course it should be easy for OP to extract these full rows in a loop, based on the $Find array, but it would be easier with a one liner.

But wait... maybe it's already possible and I missed it, everything is possible :D

Link to comment
Share on other sites

?

#include <array.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open(False)
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir&"\E.xlsx", False, False)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error opening E.xlsx")
 
$value = "2"
$a = StringRegExp(_ArrayToString(_Excel_RangeRead($oWorkbook)), '(?m)^(?:[^\|]*\|){5}' & $value & '\|.*$', 3)
Local $res[0][12]
For $i = 0 to UBound($a)-1
    _ArrayAdd($res, $a[$i])
Next
_ArrayDisplay($res)

:idiot:

Link to comment
Share on other sites

40 minutes ago, mikell said:

?

#include <array.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open(False)
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir&"\E.xlsx", False, False)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error opening E.xlsx")
 
$value = "2"
$a = StringRegExp(_ArrayToString(_Excel_RangeRead($oWorkbook)), '(?m)^(?:[^\|]*\|){5}' & $value & '\|.*$', 3)
Local $res[0][12]
For $i = 0 to UBound($a)-1
    _ArrayAdd($res, $a[$i])
Next
_ArrayDisplay($res)

:idiot:

Nice Work Mr.@mikell

Can do this without StringRegExp ?

Link to comment
Share on other sites

  • Solution
4 hours ago, ahmeddzcom said:

Can do this without StringRegExp ?

Of course you can :)
Once the $aExcel array is converted to a string then you can use StringSplit, details below

#include <array.au3>
#include <Excel.au3>

Global $oExcel = _Excel_Open(False)
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir&"\E.xlsx", False, False)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error opening E.xlsx")
 
Local $value = "2", $col = 5
$aExcel = _Excel_RangeRead($oWorkbook)
Local $res[0][UBound($aExcel, 2)]
$txt = _ArrayToString($aExcel)

#cs
$a = StringRegExp($txt, '(?m)^(?:[^\|]*\|){' & $col & '}' & $value & '\|.*$', 3)
For $i = 0 to UBound($a)-1
_ArrayAdd($res, $a[$i])
Next
#ce

$rows = StringSplit($txt, @crlf, 1)
For $i = 1 to $rows[0]
    $cells = StringSplit($rows[$i], "|", 2)
    If $cells[$col] = $value Then _ArrayAdd($res, $rows[$i])
Next
_ArrayDisplay($res)

 

Edited by mikell
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...