[comp.sys.mac.hypercard] Help putting sum of field in another

jbnash@vms.macc.wisc.edu (John B. Nash) (01/08/91)

Hypertext question...simple problem I think, not insulting I hope.


-----------------------------
|			    |	for Version 1.2.2
|   xxxxx VALUE field	    |	These two cards have the same background 	
|			    |	Suppose I want to click on the CUMULATIVE field
|   xxxxx CUMUL. field	    |	and have the sum of both VALUE fields appear
|			    |	in the CUMULATIVE field.  
-----------------------------	Is the script below on the right track?
				(background id 2763 for both cards).
-----------------------------
|			    |
|   xxxxx VALUE field	    |
|			    |
|   xxxxx CUMUL. field	    |
|			    |
-----------------------------
				
on mouseUp
  put zero into field "CUMULATIVE"
  put zero into total
  repeat with i = 1 to the number of cards in bg id 2763
    add bg field "VALUE" to total
    put total into field "CUMULUATIVE"
  end repeat
end mouseUp

		Right now, when I click on CUMULATIVE, it takes
		whatever number is in the VALUE field of the
		current card and multiplies it by the number of
		cards in the stack. Obviously this script is good if the 	
		numbers in the VALUE fields are all the same, but they are 			
		different from card to card.

		Ideas appreciated.  Just E me 
_______________________________________________________________________________
| J. B. NASH   				  Internet > jbnash@vms.macc.wisc.edu |
| University of				   	     Bitnet > jbnash@wiscmac2 |
| Wisconsin		       		   	      DECnet > wircs2::jbnash |
| Madison	          from CompuServe > internet:jbnash@vms.macc.wisc.edu |
-------------------------------------------------------------------------------

jkc@Apple.COM (John Kevin Calhoun) (01/08/91)

In article <1991Jan8.005708.5908@macc.wisc.edu> jbnash@vms.macc.wisc.edu
(John B. Nash) writes:

[ A question about summing the contents of a background field from each
  card of the background, and putting the new total into a background
  field on each card of the background. ]

Here's how I would do this in HyperCard 2.0:

>on mouseUp
   -- I removed a line here
>  put zero into total  -- initialize variable
   lock screen          -- optional step
   push card            -- remember where we started
>  repeat with i = 1 to the number of cards in bg id 2763
     go to card i of bg id 2763  -- crucial step
>    add bg field "VALUE" to total
     -- I removed a line here.  
>  end repeat
   pop card             -- go back where we started
   put total into field "CUMULATIVE"
   unlock screen        -- only if you locked the screen, above
>end mouseUp

This works if the sharedText of field "CUMULATIVE" is true.

In version 1.x, I would do it like this, because there's no
sharedText property for background fields:

>on mouseUp
   -- I removed a line here
>  put zero into total  -- initialize variable
   lock screen          -- optional step
   push card            -- remember where we started
>  repeat with i = 1 to the number of cards in bg id 2763
     go to card i of bg id 2763  -- crucial step
>    add bg field "VALUE" to total
     -- I removed a line here.  
>  end repeat
   repeat with i = 1 to the number of cards in bg id 2763
     go to card i of bg id 2763
     put total into field "CUMULATIVE"
     -- I removed a line here.  
   end repeat
   pop card             -- go back where we started
   unlock screen        -- only if you locked the screen, above
>end mouseUp

Kevin Calhoun
HyperCard Team
Apple Computer, Inc.

ralph@world.std.com (Ralph Lombreglia) (01/08/91)

From article 2867:
>repeat with i = 1 to the number of cards in bg id 2763
>add bg field "VALUE" to total

You need to tell HyperCard which card you want to work with on each
iteration.  The second line above should read:

add field "VALUE" of card i to total

("field") implies background field in HyperCard).

Ralph Lombreglia
Internet: ralph@world.std.com   MCI Mail: rlombreglia   Bix: rlombreglia