HyperActive Software

Home What's New Who We Are What We Do Solutions Resources

We make software for humans. Custom Mac, Windows, iOS and Android solutions in HyperCard, MetaCard, and RunRev LiveCode


HyperCard Tips and Tricks: Numbers and Math

Binary to Decimal Function

Posted: 5/24/98
I believe Tom Pittman, of CompileIt! fame (among other fames)
originally put me on to this neat Binary to Decimal converter:

function Bin2Dec theBin
  put 0 into theDec
  repeat until theBin is empty
    add theDec+first char of theBin to theDec
    delete first char of theBin
  end repeat
  return theDec
end Bin2Dec

This makes a lot of people go "wait a minute" until they look at it
twice and also remember that the first char is not the first (rightmost) binary digit.

Bruce Carter
SMITC, Boise State University

Changing the sign of a variable

Posted: 12/4/97
I used to multiply by -1 if I wanted to change the sign of a variable.
Then I saw a script that takes advantage of the fact that "-" is an

  put -var into var

Paul Foraker
White Feather Software

Restricting data entry

Posted: 12/4/97
You can set up a field so that the user is able to type whatever
keystrokes he feels like, but any keystrokes which don't fit some
specific criteria won't appear in the field. For example, say that you
want the user to be able to type only a 3-digit integer into card field 
"Fred". This handler will do that:

on keyDown DisUn
  if (DisUn is in "1234567890") and (the length of me < 3) then pass keyDown
end keyDown

This handler isn't perfect but it illustrates the basic concept: Set up 
some sort of qualifications for keystrokes, and don't "pass keyDown"
unless the keystroke meets your criteria.

Quentin Long
Graphic Descriptions

Processing dates

Posted: 11/28/97
When working with dates in HyperCard here are two good tips:

1. When trying to find previous or subesquent dates, always use the
convert command to change your date into the dateItems format. This is
the ONLY way to reliably move between months, days of the week, etc.
People often assume that the date format is consistent but it actually
varies widely. For example North American users typically use
month/day/year, but Europeans typically use day/month/year. Only
dateItems format, which is internal to HyperCard, is guaranteed to be
unchanging. (The dateItems format is comma-delimited list of numbers in 
this order: year, month, day, hour, minute, second, day of week (Sunday 
= 1)

2. When adding or subtracting times or dates it is invariably easier to 
use the convert command to change your date into seconds (ie
"convert the date to seconds") and simply work with the resulting

Ben Lawson

Sum behavior

Posted: 11/24/97
The SUM function breaks if you pass it empty or if any item is not a
number. But it doesn't break if you pass it empty items. You can pass 
the function a list of empty items and it returns "0". answer sum(,,,,) -- works

Paul Foraker
White Feather Software

NumberFormat oddities

Posted: 11/24/97
The numberFormat is consistent but confusing. A change in the
numberFormat affects ALL numeric variables *simultaneously* in
HyperCard, which is surprising.  But it doesn't affect string
variables, even if they happen to contain a string that looks exactly
like a number.

It's confusing because HyperCard intentionally hides the difference
between numbers and strings.  You can't tell visually whether a
container holds a number or a string that happens to LOOK like a
number.  So unless you're SURE which variables hold numbers, not
strings that look like numbers, it isn't obvious which variables will
be affected when you change the numberFormat.

Here are some examples of when numbers are stored as numbers and when
they are stored as strings instead:

put 1.23 into x             -- x holds the NUMBER 1.23
put "1" & ".23" into x      -- x holds the STRING 1.23 (due to
put x + 0 into x            -- math converts it back to the NUMBER 1.23
put 1.23 into item 1 of x   -- x holds the STRING 1.23 (item lists are strings)
put 1.23 into cd fld "foo"  -- foo holds the STRING 1.23 (fields are
put 0 into x                -- x holds the NUMBER 0
put "0" into x              -- x holds the STRING 0 (due to quotes)
put zero into x             -- x holds the STRING 0 (ZERO gives a string!)

Here are a couple of ways you can tell if you have a number or a string: 

Numbers are stored if you put an unquoted number or the result of a
math expression into an ordinary variable.

Strings are stored whenever you use concatenation, or whenever you
store anything into an item list or a field.

Anthony Rich

While technically all strings are stored within HC as text, the author
here is talking about how these strings behave. Strings that behave as
numbers respond to numberformat commands and use SANE routines for
calculations. Strings that contain numbers but behave as text do not
respond to numberformat commands. -- jg

Randoms within a range

Posted: 11/24/97
Use of the random function as in random(10) returns a number in the
range 1 to 10. You can adjust the range by using the following formula:

   random(highEnd-lowEnd+1)+(lowEnd-1) -- thus
   random(16)+4 -- returns numbers between 5 and 20 inclusive
   random(101)+99 -- returns numbers between 100 and 200

Greg Mcilhiney

Up to top | HyperCard Tips and Tricks