xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

AClone()

Creates a deep copy of an array.

Syntax

AClone( <aArray> ) --> aClone

Arguments

<aArray>
A variable holding the array to duplicate.

Return

The function returns a reference to the created array.

Description

AClone() is an array function that duplicates an array. Unlike ACopy(), which operates only on one-dimensional arrays, AClone() performs a deep copy and iterates through all dimenstions of an array. If an array element contains an array, this sub-array is duplicated as well. As a result, all array references contained in one array are duplicated, resulting in a deep copy of the source array.

Note  Array elements holding values of complex data types other than type Array are not duplicated. That is, Hashes, Code blocks and Objects are not duplicated. AClone() transfers only their reference to the result array. Source and result array then contain the exact same value of the complex data type.

Info

See also:ACopy(), ADel(), AIns(), ASize()
Category: Array functions
Source:vm/arrayshb.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// This example demonstrates the difference between ACopy() and AClone()
//   -> aTgt1 is a shallow copy of the source array
//   -> aTgt2 is a deep copy of the source array

   PROCEDURE Main
      LOCAL aSrc  := { 1, { "A", "B" } }
      LOCAL aTgt1 := ACopy( aSrc, Array(2) )
      LOCAL aTgt2 := AClone( aSrc )

      // check first element of sub-array
      ? aTgt1[2,1]                     // result: A
      ? aTgt2[2,1]                     // result: A

      // change first element of sub-array in source array
      aSrc[2,1] := "no clone"

      // re-check first element of sub-array
      ? aTgt1[2,1]                     // result: no clone
      ? aTgt2[2,1]                     // result: A
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe