Page 1 of 1

Dividir Un Array "SOLUCIONADO"

PostPosted: Thu May 20, 2021 5:38 pm
by remtec
Amigos muy buen dia.

Tengo un Array que sellena con una cantidad variable de registros.

Ejemplo: Arreglo A = Contiene 90 Registro.

Necesito Dividir Arreglo A, en 3 partes, por lo que:
Arreglo B, C y D, deben contener 30 registros cada uno

Muchos Saludos.

Antonio.

Re: Dividir Un Array

PostPosted: Fri May 21, 2021 6:46 am
by JESUS MARIN
2 preguntas

Y si el numero total de elementos del array A, no es divisible entre 3 ?

Necesitas los 30 primeros en el array B, los 30 intermedios en el array C, y los 30 ultimos en el array D ?

Re: Dividir Un Array

PostPosted: Fri May 21, 2021 11:01 am
by hmpaquito
aCopy es la manera más rápida de hacer eso:

ACopy()
Copy elements from one array to another

Syntax
ACopy( <aSource>, <aTarget>, [<nStart>], [<nCount>], [<nTargetPos>] ) → aTarget

Arguments
aSource is the array to copy elements from.
aTarget is the array to copy elements to.
nStart is the beginning subscript position to copy from aSource
nCount the number of subscript elements to copy from aSource.
nTargetPos the starting subscript position in aTarget to copy elements to.

Returns
aTarget an array pointer reference

Re: Dividir Un Array

PostPosted: Fri May 21, 2021 11:39 am
by cnavarro
Otra posibilidad
Code: Select all  Expand view


   local aA     := { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
   local nDiv   := 3
   local aB     := {}, aC     := {}, aD     := {}

   AEVal( aA, { | e, n | if( n <= nDiv, AAdd( aB, e ), if( n <= nDiv * 2, AAdd( aC, e ), AAdd( aD, e ) ) ) } )

   XBrowse( aB )
   XBrowse( aC )
   XBrowse( aD )

 

Re: Dividir Un Array

PostPosted: Fri May 21, 2021 2:25 pm
by xmanuel
Corroboro la idea de Paquito...
Este ejemplo crea elementos al azar:

Code: Select all  Expand view

procedure main

    local A := Array( Int( hb_Random( 1, 300 ) ) )
    local B, C, D  // Arrays adicionales
    local nLen := Len( A )  // Elementos del array principal
    local nPar := Round( nLen / 3, 2 ) // Calculo de elementos para los 3 arrays adicionales
    local i // Contador

    ? "Array creado al azar tiene " + HB_NToS( nLen ) + " elementos..."
    ? "Ahora lo vamos a rellenar... Presiona ENTER para empezar"
    Inkey( 100 )

    for i := 1 to nLen
        A[ i ] := HB_NToS( hb_Random( 1, 1000 ) )    // Relleno del array principal
    next

    // Ver el array principal
    cls
    ? "Ver array general -> A:"
    AChoice( 02, 02, 22, 12, A )

    // Troceo del array principal en los tres, el ultimo se saca por diferencia:
    // total elemento del array principal meno la suma del LEN de los 2 primeros
    B := Array( nPar )
    ACopy( A, B, 1, nPar )
    C := Array( nPar )
    ACopy( A, C, nPar + 1, nPar )
    D := Array( nLen - ( 2 * nPar ) )
    ACopy( A, D, ( nPar * 2 ) + 1, nLen - ( 2 * nPar ) )

    // Ver los array partidos
    cls
    ? "Array B "
    Achoice( 02, 02, 22, 12, B )
    cls
    ? "Array C "
    Achoice( 02, 02, 22, 12, C )
    cls
    ? "Array D "
    Achoice( 02, 02, 22, 12, D )

return
 

Re: Dividir Un Array

PostPosted: Sat May 22, 2021 5:12 pm
by remtec
Amigos.

Mil gracias a todos por su tremenda ayuda.

Aplique la sugerencia del Maestro Navarro.

a la pregunta de Jesús.
Efectivamente puede suceder que no sea divisible por 3, si menor a 3, asignare 1 array1 y 1 array2 y así sucesivamente, la idea seria dividirlos en partes iguales, si no se puede, se irán llenando como como preguntas, de array1, array2 y array3 el saldo.

Hasta aquí funciona muy bien, pero hare muchas pruebas para llevarlo a todas las posibles situaciones.

Reitero las muchas gracias a todos.

Saludos
Antonio