Welcome, Guest
Username: Password: Remember me
Visual Objects

Please use this forum to post questions about Visual Objects and Vulcan.NET
  • Page:
  • 1

TOPIC:

Extract column from array 18 Jul 2022 12:12 #23053

  • Jack@007
  • Jack@007's Avatar
  • Topic Author


  • Posts: 4
  • Hi,

    I'm a little bit rusty in VO.

    My question concerns a multi dimensional array like:

    a:={}
    AAdd(a, {'a1', 'b1', 'c1'})
    AAdd(a, {'a2', 'b2', 'c2'})
    AAdd(a, {'a3', 'b3', 'c3'})

    Extracting a row from the array is easy.
    b:={}
    b:=a[2]

    (b equal "a2", "b2", "c2" ).

    What would be the most efficiƫnt way to extract a column from an array (so the "b1", "b2", "b3" values)?
    I'm not getting further than a for next loop.

    TIA

    Jack

    Please Log in or Create an account to join the conversation.

    Extract column from array 18 Jul 2022 13:57 #23056

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3199
  • Hi Jack,
    this could be done in a similar way:
    b:={}
    for nI := 1 upto ALen( a )
      AAdd( b, a[nI,2] )
    next
    Please not that this is not optimal code, it should only give you an idea.
    If you need optimized code for this, please let me know.
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Last edit: by wriedmann.

    Extract column from array 18 Jul 2022 15:33 #23058

    • leon-ts
    • leon-ts's Avatar


  • Posts: 238
  • Hi Jack,
    The following examples are not optimized, but visually simple:

    Example 1
    LOCAL b := {} AS ARRAY // initialized array
    AEval(a, {|x| AAdd(b, x[2])})

    Example 2
    LOCAL b AS ARRAY // NULL_ARRAY
    b := AEvalA(AClone(a), {|x| x[2]})

    Best regards,
    Leonid

    Please Log in or Create an account to join the conversation.

    Extract column from array 18 Jul 2022 15:54 #23060

    • Jack@007
    • Jack@007's Avatar
    • Topic Author


  • Posts: 4
  • Hi Wolfgang,

    That approach is what i'm using now too.

    Please Log in or Create an account to join the conversation.

    Extract column from array 18 Jul 2022 15:55 #23061

    • Jack@007
    • Jack@007's Avatar
    • Topic Author


  • Posts: 4
  • Hi Leonid,

    That's more the way i was looking for; a one liner to fill the new array.
    Thanks.

    Please Log in or Create an account to join the conversation.

    Extract column from array 18 Jul 2022 15:59 #23062

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3199
  • Hi Jack,
    if your arrays are small, the one liner works very well.
    But if your arrays are larger, the one liners are using more memory.
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Extract column from array 18 Jul 2022 16:10 #23063

    • Jack@007
    • Jack@007's Avatar
    • Topic Author


  • Posts: 4
  • Hi Wolfgang,

    The array i'm using has typical dimensions of 30x7 elements. I guess memory should be no problem. Correct me i i'm wrong.

    Please Log in or Create an account to join the conversation.

    Extract column from array 18 Jul 2022 16:21 #23064

    • wriedmann
    • wriedmann's Avatar


  • Posts: 3199
  • Hi Jack,
    with such small arrays that should not be a problem.
    If the arrays become larger, creating the target array with the correct size helps a lot.
    That would result in code like this:
    local nLen as dword
    nLen := ALen( a )
    b := ArrayNew( nLen )
    for nI := 1 upto nLen
      b[nI] := a[nI,2]
    next
    Wolfgang
    Wolfgang Riedmann
    Meran, South Tyrol, Italy

    www.riedmann.it - docs.xsharp.it

    Please Log in or Create an account to join the conversation.

    Last edit: by wriedmann.

    Extract column from array 18 Jul 2022 16:30 #23065

    • leon-ts
    • leon-ts's Avatar


  • Posts: 238
  • Guys,
    I think the best option is to write a function. It will be optimal both in terms of speed and memory, and will also be readable when used in code (one line).

    The fastest and most optimized variant of the function (as far as I know VO):
    FUNCTION ACol(aSource AS ARRAY, dwElement AS DWORD) AS ARRAY PASCAL
    	
        LOCAL i, dwLen AS DWORD
        LOCAL aResult, aI AS ARRAY
    	
        IF ( dwLen := ALen(aSource) ) > 0
            aResult := ArrayCreate(dwLen)
            FOR i := 1 UPTO dwLen
                aI := ArrayGet(aSource, i)
                ArrayPut(aResult, i, ArrayGet(aI, dwElement))
            NEXT
        ELSE
            aResult := {}
        ENDIF
    	
        RETURN aResult

    Applying the ACol function in code:
    LOCAL b AS ARRAY
    b := ACol(a, 2)

    Best regards,
    Leonid

    Please Log in or Create an account to join the conversation.

    Last edit: by leon-ts.
    • Page:
    • 1