unittable.frink

Download or view unittable.frink in plain text format


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

  To use this with a filename, call it as:
  parseURLWithUnits[filenameToURL[filename]]

  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.
*/

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

   LINE:
   for line = lines[URL]
   {
      // 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

      // Blank line, skip
      if line =~ %r/^\s*$/
         next
      
      nums = eval[split[delimiter, line]]
      if (unitArray != undef)
         nums = mul[unitArray, nums]   // mul exists in 2025 and later releases
      
      result.push[nums];
   }
   
   return result
}

/** Parse a filename with units.
    This is just a wrapper around parseURLWithUnits.
*/

parseFileWithUnits[filename] :=
{
   return parseURLWithUnits[filenameToURL[filename]]
}

// Example usage, including fetching from a URL:
// println[parseURLWithUnits["https://frinklang.org/unittable.txt"]]


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, eliasen@mindspring.com