Yeah could be the beers, when I said clone… I meant clone.
I knew it! :)
the only way you could avoid copying everything would be in a case like:
poo.shape=["sphere","sphere","sphere"];
poo.colors=["grey","brown","white"];
and you would only want to change the colors. then this would work:
pooCopy.shape=poo.shape;
pooCopy.colors=["blue","red","green"];
but there is no way to just change one value in any of those arrays
without copying or replacing the whole thing.
On Apr 12, 2010, at 2:30 AM, Alastair Leith wrote:
Yeah could be the beers, when I said clone… I meant clone.
Thanks for the interest though. cwright replied to me privately at
one point so the conversation may have got confusing. I got into the
muddle because i was trying to avoid duplicating the whole structure
'by hand' and then I tried just editing the data I I wanted to. I
assume editing the Input Structure receving the structure from the
first JS patch (and thus avoiding a duplication loop) was a no_no,
but I'l try it just to see.
Best
Alastair
2010/4/12 Boštjan Čadež <[email protected]>
for some reason (prolly the couple of beers i had) i think you got
some terminology mixed up. what do you mean when you say clone? for
me a clone would mean a copy, which in this case would be a good
thing. you can edit a copy. the problem you encountered happens with
references. to be sure of what im trying to say ill go into to much
detail.
lets say you have a structure like (in this case an object):
poo={};
poo.color="brown";
poo.weight=2.3;
poo.shape=["sphere","sphere","cylinder"];
lets say you have this in one JS patch and output it to another one
via noodle. in the one that receives you have:
pooCopy=poo;
pooCopy.weight=2.1; FAIL
this doesnt work because "pooCopy=poo;" just copies the memory
address (a number) for the object (aka reference) not the whole
object (many numbers). so "pooCopy.weight=2.1" still tries to change
the object you cant change, because inputs are read only.
second try:
pooCopy={};
pooCopy.color=poo.color;
pooCopy.weight=poo.weight;
pooCopy.shape=poo.shape;
pooCopy.color="grey";
pooCopy.weight=2.1;
pooCopy.shape=["sphere","sphere","sphere"]; this works
but if you tried this instead:
pooCopy.shape[2]="sphere"; FAIL
this is because poo.shape is an object also. and
"pooCopy.shape=poo.shape;" puts a reference for poo.shape at
pooCopy.shape which you cant change because its read only.
pooCopy={};
pooCopy.color=poo.color;
pooCopy.weight=poo.weight;
pooCopy.weight=[];
for(var i=0;i<poo.shape.length;i++)
{
pooCopy[i]=poo[i];
}
this would do the job. basically, you have to copy each property of
an object of an object of an object / element of an array of an
array of an array "by hand".
i once found a recursive function that would add the copy ability to
the object prototype, but that does ugly stuff to the output ports.
IMHO its better to make your own copy function for your custom
structure, or in the case above, make just one javascript patch and
avoid the copying.
p.s. beer makes me talk to much;)
On Apr 11, 2010, at 5:21 PM, Alastair Leith wrote:
Yes okay, so I can do this kind of iteration in the main function
(often I do it that way but sometimes in a function call) and still
get cloning not referencing right?
Yeah an old screen shot saved me on my last project ;) One day I
would like to get to the bottom of that. I was able to write the JS
in a fresh patch carefully testing for parsing as I went and I got
it functioning but the exact same text was jamming in another JS
patch no matter how much I retyped it. I feel the JS patch is a
little illogical at times (as in it gets broken), if that's a good
way to put it.
Oops, yes recursive is what I meant but it seems to be a regressive
move in JS too.
On 12 April 2010 01:12, Alastair Leith <[email protected]>
wrote:
---------- Forwarded message ----------
From: Alastair Leith <[email protected]>
Date: 12 April 2010 01:06
Subject: Re: Javascript issue modifying a structure (again… :/)
To: Christopher Wright <[email protected]>
Yes that helps heaps. Thanks cwright. I kind of twigged to the
referencing thing a few weeks back but didn't really extrapolate
the general rule (I was getting some strange results!) and still
not 100% sure I have yet. So…
Does the function you wrote also assign individual item references
back to the original structure? If they are values and not
references put into newObj, is that distinction because of the call
to another function or just the iterating through at the lowest
child level of the structure? If items had sub-items would I need
to iterate though sub-items (in more function calls ? regressive
function calls are out in JS aren't they?)?
So many questions! I'm really grateful for your help already
though, thanks again.
Alastair
I guess the downside of a loosely typed language like JS when your
trying to teach yourself scripting (that's me btw ;) ) is that
things can get a little slippery and not generate meaningful error
mesages...
On 12 April 2010 00:49, Christopher Wright <[email protected]>
wrote:
If anybody could identify my misconception here I be very grateful:
You're assuming that you can modify inputs -- you can't.
You're assuming that JS copies by value -- it doesn't (it copies by
reference).
So, when you assign glyph to an input: glyph =
glyphs[cursor]; glyph is a reference to the input -- trying to
modify that will fail.
You need a copy function, as follows:
function copy(obj)
{
var newObj = new Object();
for(var e in obj)
newObj[e] = obj[e];
return newObj;
}
cursor = 5;
function (__structure Glyphs_Out, __boolean Same, __string Font,
__structure Glyph)
main (__structure Glyphs_In)
{
var result = new Object();
var glyph = new Object();
var glyphs = new Array();
var font = new String();
font = "LucidaGrande";
if (!_testMode)
{
glyphs = Glyphs_In;
glyph = copy(glyphs[cursor]);
// This line works:
font = glyph.Font_Name;
glyph.Font_Name = font;
glyph.Y +=0.1;
// Can't modify inputs, so you'll have to copy some
other inputs too.
//glyphs[cursor]["Y"] +=0.1;
result.Font = font;
result.Glyph = glyph;
result.Glyphs_Out = glyphs;
return result
}
}
Hope that helps! :)
--
[ christopher wright ]
[email protected]
http://kineme.net/
--
[ christopher wright ]
[email protected]
http://kineme.net/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartzcomposer-dev mailing list ([email protected]
)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quartzcomposer-dev/fskolog%40gmail.com
This email sent to [email protected]
lp,
fšk
lp,
fšk
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartzcomposer-dev mailing list ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quartzcomposer-dev/archive%40mail-archive.com
This email sent to [email protected]