Copy ARRAY filtered by column

Copy ARRAY filtered by column

Postby Otto » Tue Nov 15, 2022 8:05 am

Dear friends,

What is the most effective way to copy certain columns from an ARRAY to a new ARRAY:
eg ARRAY {"col1", "col2", "col3", "col4", "col5"}

new ARRAY {"col1", "col3", "col5"}

Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: Copy ARRAY filtered by column

Postby Otto » Tue Nov 15, 2022 6:57 pm

Hello friends,
the code is for FIVEWIN and modHarbour.

This is how to use in modHarbour.

Best regards.

Otto
Image


Image



Code: Select all  Expand view

// Copy an array select cols and col sorting

#include "FiveWin.ch"

//----------------------------------------------------------------------------//

function Main()
    local aOrg := { {"1col1","col2","col3","col4","1col5","col6","col7","1col8"     },;
                    {"2col1","col2","col3","col4","2col5","col6","col7","2col8"     },;
                    {"3col1","col2","col3","col4","3col5","col6","col7","3col8"     }}
   
    local aData := {}
    local I := 0
    local nIdx := 0
    local aCols := {}
    local aTmp := {}
    local nCol := 0
     
    local hxBrowse := {=>}

     hxBrowse["acol"] := {2,1}


   if  Empty(   HGet( hxBrowse, "acol") ) = .F.
      aCols := hxBrowse["acol"]
   endif


    //aCols empty - all cols copied
       
    //ROW
    FOR I := 1 to len( aOrg )
        if empty(aCols) = .f.
            //cols sorting and selecting
           
            aTmp := {}
            FOR nIdx := 1 to len( aCols )
                nCol := aCols[ nIdx ]
               
                AADD( aTmp, aOrg[ I ][ nCol ] )
            NEXT
            AADD( aData,  EVAL( ArrayGetBlock( aTmp , 1,"test")  ) )
        else
            AADD( aData,  EVAL( ArrayGetBlock( aOrg[ I ], 1,"test" )  ) )
        endif
       
    NEXT
   
    xbrowse( aData )
   
return nil
//----------------------------------------------------------------------------//

function ArrayGetBlock( aGets, i, cText )
local aTmp := ACLONE( aGets )
return { | u | aTmp  }

 
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6332
Joined: Fri Oct 07, 2005 7:07 pm

Re: Copy ARRAY filtered by column

Postby nageswaragunupudi » Wed Nov 16, 2022 7:13 pm

1) Using FWH function ArrayTransposeQ()

Code: Select all  Expand view

aData := .... your array
aNew  := ArrTransposeQ( aData )
HB_ADel( aNew, 4, .t. )
HB_ADel( aNew, 2, .t. )
aNew  := ArrTranposeQ( aNew )
 

a) This is not efficient
b) You do not use FWH functions in modHarbour.

2) AEval
Code: Select all  Expand view

aNew := {}
AEval( aOriginal, { |a| AAdd( aNew, { a[ 1 ], a[ 3 ], a[ 5 ] } ) } )
 
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10643
Joined: Sun Nov 19, 2006 5:22 am
Location: India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 85 guests