unittable.frink

Download or view unittable.frink in plain text format


/*
  This reads a file with units defined in a special line that begins
  with #!

  A delimiter may be passed in as either a string or a regular expression.
  By default, this splits on whitespace.

  This function returns a two-dimensional array, with each row being
  one line from the file, with units multiplied in.

  This may be used with a text file like:
  https://frinklang.org/unittable.txt

  Also note that if your data file contains the units of measure with each
  column, like "3 m/s", then this whole file becomes totally irrelevant and
  you can parse the fields using Frink's "eval" statement with almost zero
  work.
*/

parseFileWithUnits[filename, delimiter = %r/\s+/] :=
{
   result = new array
   unitArray = undef

   LINE:
   for line = lines["file:$filename"]
   {
      Lines beginning with #! contain units
      if [units] = line =~ %r/^\s*#!\s*(.*)/
      {
         unitArray = eval[split[delimiter, units]]
         next
      }

      Other lines beginning with # are comments
      if (line =~ %r/^\s*#/)
         next
      
      nums = eval[split[delimiter, line]]
      if (unitArray != undef)
         nums = multiplyVector[unitArray, nums]
      result.push[nums];
   }
   return result
}

// Multiply two vectors and return the result.
multiplyVector[a1, a2] :=
{
   u = min[length[a1], length[a2]]
   res = new array
   for c = 0 to u-1
      res@c = a1@c * a2@c
   return res
}


Download or view unittable.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 19945 days, 14 hours, 35 minutes ago.