assign by reference (arrays)?

Ben Rubinstein benr_mc at cogapp.com
Thu Mar 21 11:54:01 EST 2002


I'm starting to regret this message... it's certainly not the end of the
world, and I'll withdraw the term counter-intuitive.  But I just wanted to
know if this facility was available.

Here - with the clear understanding that I agree that there are other ways
to skin this cat - is what I was on about.

Let's say that I'm processing messages to the use-revolution and
improve-revolution mailing lists.  For each one I want to find out how many
replies were received.  So my code might look like this:

  on doit msgdata
    put empty into aUseInfo
    put empty into aImproveInfo
    repeat for each line msgrec in msgdata
        put item 1 of msgrec into msgID
        put item 2 of msgrec into msgReplyID
        put item 3 of msgrec into msgList
        switch msgList
        case "use"
            noteInfo(aUseInfo, msgID, msgReplyToID)
            break
        case "improve"
            noteInfo(aImproveInfo, msgID, msgReplyToID)
            break
        end switch
    end repeat
  end doit

  on noteInfo @aInfo, mID, rID
    get aInfo[rID]
    if it = empty then
        put 1 && mID into aInfo[rID]
    else
        add 1 to word 1 of it
        put it && mID into aInfo[rID]
    end if
  end noteInfo

This works fine, because I can pass the actual array into the 'noteInfo'
handler, by virtue of declaring the first parameter by reference.  But
suppose for some reason I don't want to use a separate handler - perhaps
because the code isn't as simple as shown in the 'noteInfo' handler here,
and needs to refer to lots of locals declared in 'doit' , for example?  My
first attempt was the equivalent of


  on doit msgdata
    put empty into aUseInfo
    put empty into aImproveInfo
    repeat for each line msgrec in msgdata
        put item 1 of msgrec into msgID
        put item 2 of msgrec into msgReplyID
        put item 3 of msgrec into msgList
        switch msgList
        case "use"
            put aUseInfo into aInfo
            break
        case "improve"
            put aImproveInfo into aInfo
            break
        end switch
        get aInfo[rID]
        if it = empty then
            put 1 && mID into aInfo[rID]
        else
            add 1 to word 1 of it
            put it && mID into aInfo[rID]
        end if
   end repeat
  end doit

But that doesn't work, because 'aInfo' is each time a fresh copy of either
the 'aUseInfo' array, or the 'aImproveInfo' array.

So I was just asking if there was some way to make aInfo point to the
actuall array, rather than a copy of it.  If there isn't, that's fine.
As various people have pointed out (thanks to all who did), there are a
number of alternatives, such as putting the name of the relevant variable
into 'aInfo' and using "do", or using a single multi-dimensional array in
the first place.  So that's OK then.

!
 
  Ben Rubinstein               |  Email: benr_mc at cogapp.com
  Cognitive Applications Ltd   |  Phone: +44 (0)1273-821600
  http://www.cogapp.com        |  Fax  : +44 (0)1273-728866






More information about the use-livecode mailing list