On 09/18/2015 04:38 PM, Robert Hanson wrote:
> Oh, I mistyped. I did not mean to add that colon.
>
> Use of "*" and/or "," leads to copying, but we may want to rethink how that
> works. *only that level* is copied. If there are references in there to
> deeper levels, those levels are not copied. I have to think if there is a
> straightforward way to do a deep copy. Right now it's a bit of a mixture.
>
I tried to build my own (multi-level) hash copying function with Jmol
script but wasn't succesful, even without trying to get a deep copy.
In the long run I will need a deep copy function but currently it would
be sufficient to copy only the single-level values (e.g.: number,
string, boolean) of the top level as values and multi-level values
(e.g.: hash, array) as references.
In my example below (copying the 'atomInfos' subhash of 'globalHash')
that would mean to copy the 'seqNum' values as values and the 'SNPs'
array as array reference.
The example hash has a few extra levels because I didn't want to get any
surprises when I would switch to 'real world examples' that have several
upper levels that will not be copied.
The documentation says that adding '@' in math context before the
variable name would result in copying the value. For simple cases it
seems to work, even without the '@':
-----------------
x = 1;
y = @x;
x = 2;
print "x=" + x + " y=" + y;
x=2 y=1
-----------------
x = 1;
y = x;
x = 2;
print "x=" + x + " y=" + y
x=2 y=1
-----------------
x = [a: 1, b: 2];
y = @x["a"];
x["a"] = 3;
print "x=" + x + "\ny=" + y;
x={
"a" : 3
"b" : 2
}
y=1
-----------------
x = [a: 1, b: 2];
y = x["a"];
x["a"] = 3;
print "x=" + x + "\ny=" + y
x={
"a" : 3
"b" : 2
}
y=1
-----------------
But within my function I couldn't get it to work:
------- Example --------------------------
function copyHashKeys(sourceHash, destinationHash, keyList) {
if (keyList.type != "array") {
keyList = sourceHash.keys;
}
if (keyList.size > 0) {
for (var currentKey in keyList) {
destinationHash[currentKey] = @sourceHash[currentKey];
}
}
}
function copyHash(sourceHash) { # IMPORTANT: no deep copy
var destinationHash = {};
if (sourceHash.type == "hash") {
copyHashKeys(sourceHash, destinationHash);
}
return destinationHash;
}
function renumber(atomInfos) {
for (var atomId in atomInfos) {
atomInfos[atomId]..seqNum *= 10;
}
}
globalHash = [subsets: [subset1: [atomInfos: [atom1: [seqNum: 1],
atom2: [seqNum: 2],
atom3: [seqNum: 3, SNPs:
["snp1","snp2"]]
]
],
subset2: {}
]
];
globalHash..subsets..subset2..atomInfos =
copyHash(globalHash..subsets..subset1..atomInfos);
renumber(globalHash..subsets..subset2..atomInfos);
print "====== Subset 2 ===========";
print globalHash..subsets..subset2..atomInfos;
print "====== Subset 1 ===========";
print globalHash..subsets..subset1..atomInfos
----- Result -----------------------------
====== Subset 2 ===========
{
"atom1" :
{
"seqNum" : 10
}
"atom2" :
{
"seqNum" : 20
}
"atom3" :
{
"SNPs" :
[
snp1
snp2
]
"seqNum" : 30
}
}
====== Subset 1 ===========
{
"atom1" :
{
"seqNum" : 10
}
"atom2" :
{
"seqNum" : 20
}
"atom3" :
{
"SNPs" :
[
snp1
snp2
]
"seqNum" : 30
}
}
------------------------------------------
I also tried other variants without success:
--- Variant 1 -------
var value = sourceHash[currentKey];
destinationHash[currentKey] = @value;
--- Variant 2 -------
var value = @sourceHash[currentKey];
destinationHash[currentKey] = value;
--- Variant 3 -------
var value = @sourceHash[currentKey];
destinationHash[currentKey] = @value;
--- Variant 4 -------
destinationHash[currentKey] = sourceHash[currentKey];
--- Variant 5 -------
var value = sourceHash[currentKey];
destinationHash[currentKey] = value;
----------------------------------
Regards,
Rolf
--
Rolf Huehne
Postdoc
Leibniz Institute for Age Research - Fritz Lipmann Institute (FLI)
Beutenbergstrasse 11
07745 Jena, Germany
Phone: +49 3641 65 6205
Fax: +49 3641 65 6210
E-Mail: [email protected]
Website: http://www.fli-leibniz.de
Scientific Director: Prof. Dr. K. Lenhard Rudolph
Head of Administration: Dr. Daniele Barthel
Chairman of Board of Trustees: Dennys Klein
VAT No: DE 153 925 464
Register of Associations: No. 230296, Amtsgericht Jena
Tax Number: 162/141/08228
------------------------------------------------------------------------------
_______________________________________________
Jmol-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jmol-users