Jump to content

Merge 2 elements of 1d array


Siwa
 Share

Recommended Posts

Hi autoit community, I have a 1d array with 6 elements, ( first column is the number of columns inside the 1d array ( which is 5 ) ). Sometimes the array has 7 elements and I have to merge the 2nd and 3rd columns into the 2nd array and the result will be a 6 element array again.

Most of the time ==> [     6     ,     a     ,     b     ,     c     ,     d     ,     e     ]

On Special occasions  ==> [     7     ,     a     ,     b     ,     c     ,     d     ,     e     ,     f     ] must become ==> [     6     ,     a b     ,     c     ,     d     ,     e     ,     f     ]

Edited by Siwa
Link to comment
Share on other sites

So what is the issue ?

Post some code.

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - 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
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

 

 

50 minutes ago, Siwa said:

I have a 1d array with 6 elements, ( first column is the number of columns inside the 1d array ( which is 5 ) )

You confuse columns with rows. A 1D array only has one column, the elements are placed in the rows.

You are using a so called 1-based array which means, that the first element (Row[0]) contains the number of entries (1 to Row[0]).

50 minutes ago, Siwa said:

Most of the time ==> [     6     ,     a     ,     b     ,     c     ,     d     ,     e     ]

This should probably be [5, a, b, c, d, e ] ;).

#include <Array.au3>
Global $sData, $aData
$sData = 'a,b,c,d,e'
$aData = StringSplit($sData, ',')
_ArrayDisplay($aData)

@Siwa : EDIT

On Special occasions  ==> [7,a,b,c,d,e,f] must become ==> [6,ab,c,d,e,f]

On Special occasions  ==> [6,a,b,c,d,e,f] must become ==> [5,ab,c,d,e,f]

I recommend to read a tutorial regarding arrays first, e.g. : https://www.autoitscript.com/wiki/Arrays

Edited by Musashi
typo

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Hey Siwa,

This should do it, unless there is a more efficient way.

#include <Array.au3>

main()

func main()

    ;local $aArray[6] = ["6","a","b","c","d","e"]
    local $aArray[7] = ["7","a","b","c","d","e","f"]

    if ubound($aArray) = 7 Then  ; Change number according to your need
        local $aTemp[2]

        _ArrayConcatenate($aTemp,_ArrayExtract($aArray,3,-1,0,0))

        $aTemp[1] = $aArray[1] & $aArray[2]  ; Change according to your need
        $aTemp[0] = ubound($aTemp)

        $aArray = $aTemp  ; Assign back to original array
    EndIf

    _ArrayDisplay($aArray,"test")

EndFunc

 

Link to comment
Share on other sites

22 minutes ago, GokAy said:

    local $aArray[7] = ["7","a","b","c","d","e","f"]

If you work with 1-based arrays, this notation is ambiguous.

Yes, the array has 7 elements, but element [0] contains the number of elements from 1 to element [0], which is 6.

In your example it works because you are using UBound but if you loop with 1 to element[0] you will get the error : ==> Array variable has incorrect number of subscripts

#include <Array.au3>
main()
func main()
    Local $aArray[7] = ["7","a","b","c","d","e","f"]  ; ==> Error 
    ;Local $aArray[7] = ["6","a","b","c","d","e","f"] ; ==> OK

    For $i = 1 To $aArray[0]
        ConsoleWrite("Element " & $i & " = " & $aArray[$i] & @CRLF)
    Next
EndFunc

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Hey @Musashi, thanks for pointing that out.

Problem is, there is conflicting info in the first post. However, it does what Siwa has asked for?!

3 hours ago, Siwa said:

Hi autoit community, I have a 1d array with 6 elements, ( first column is the number of columns inside the 1d array ( which is 5 ) ). Sometimes the array has 7 elements and I have to merge the 2nd and 3rd columns into the 2nd array and the result will be a 6 element array again.

Most of the time ==> [     6     ,     a     ,     b     ,     c     ,     d     ,     e     ]

On Special occasions  ==> [     7     ,     a     ,     b     ,     c     ,     d     ,     e     ,     f     ] must become ==> [     6     ,     a b     ,     c     ,     d     ,     e     ,     f     ]

I took the numbering from the examples. @Siwa, should they be 5 and 6 instead?

Just change this part accordingly too.

$aTemp[0] = ubound($aTemp)
Link to comment
Share on other sites

5 hours ago, Musashi said:

 

 

You confuse columns with rows. A 1D array only has one column, the elements are placed in the rows.

You are using a so called 1-based array which means, that the first element (Row[0]) contains the number of entries (1 to Row[0]).

This should probably be [5, a, b, c, d, e ] ;).

#include <Array.au3>
Global $sData, $aData
$sData = 'a,b,c,d,e'
$aData = StringSplit($sData, ',')
_ArrayDisplay($aData)

@Siwa : EDIT

On Special occasions  ==> [7,a,b,c,d,e,f] must become ==> [6,ab,c,d,e,f]

On Special occasions  ==> [6,a,b,c,d,e,f] must become ==> [5,ab,c,d,e,f]

I recommend to read a tutorial regarding arrays first, e.g. : https://www.autoitscript.com/wiki/Arrays

I try to, and I wanted to use the native functions instead of polluting my code with dozens of IF statements. 

 

4 hours ago, GokAy said:

Hey Siwa,

This should do it, unless there is a more efficient way.

#include <Array.au3>

main()

func main()

    ;local $aArray[6] = ["6","a","b","c","d","e"]
    local $aArray[7] = ["7","a","b","c","d","e","f"]

    if ubound($aArray) = 7 Then  ; Change number according to your need
        local $aTemp[2]

        _ArrayConcatenate($aTemp,_ArrayExtract($aArray,3,-1,0,0))

        $aTemp[1] = $aArray[1] & $aArray[2]  ; Change according to your need
        $aTemp[0] = ubound($aTemp)

        $aArray = $aTemp  ; Assign back to original array
    EndIf

    _ArrayDisplay($aArray,"test")

EndFunc

 

This code does what I want. But, because of my little and limited knowledge of Computer Programing Language, I must dig deeper into it to understand it more. For now I'm done. Thanks for your help. 🙏

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