From: Hugh Sasse Date: 2006-10-27T22:59:26+09:00 Subject: Re: reading variables in a file On Fri, 27 Oct 2006, Ooo Grec wrote: > I'm thinking of migrating from Fortran to Ruby. Do you think i should go > ahead?? You might just want to do the text processing in ruby, and invoke fortran programs for the numeric bits. You've got answers to the other bit. If you want to write fixed length records, which IIRC Fortran is good at reading, you can use (if you've done make install-doc you can get this with `ri pack`): ------------------------------------------------------------- Array#pack arr.pack ( aTemplateString ) -> aBinaryString ------------------------------------------------------------------------ Packs the contents of _arr_ into a binary sequence according to the directives in _aTemplateString_ (see the table below) Directives ``A,'' ``a,'' and ``Z'' may be followed by a count, which gives the width of the resulting field. The remaining directives also may take a count, indicating the number of array elements to convert. If the count is an asterisk (``+*+''), all remaining array elements will be converted. Any of the directives ``+sSiIlL+'' may be followed by an underscore (``+_+'') to use the underlying platform's native size for the specified type; otherwise, they use a platform-independent size. Spaces are ignored in the template string. See also +String#unpack+. a = [ "a", "b", "c" ] n = [ 65, 66, 67 ] a.pack("A3A3A3") #=> "a b c " a.pack("a3a3a3") #=> "a\000\000b\000\000c\000\000" n.pack("ccc") #=> "ABC" Directives for +pack+. Directive Meaning --------------------------------------------------------------- @ | Moves to absolute position A | ASCII string (space padded, count is width) a | ASCII string (null padded, count is width) B | Bit string (descending bit order) b | Bit string (ascending bit order) C | Unsigned char c | Char D, d | Double-precision float, native format E | Double-precision float, little-endian byte order e | Single-precision float, little-endian byte order F, f | Single-precision float, native format G | Double-precision float, network (big-endian) byte order g | Single-precision float, network (big-endian) byte order H | Hex string (high nibble first) h | Hex string (low nibble first) I | Unsigned integer i | Integer L | Unsigned long l | Long M | Quoted printable, MIME encoding (see RFC2045) m | Base64 encoded string N | Long, network (big-endian) byte order n | Short, network (big-endian) byte-order P | Pointer to a structure (fixed-length string) p | Pointer to a null-terminated string Q, q | 64-bit number S | Unsigned short s | Short U | UTF-8 u | UU-encoded string V | Long, little-endian byte order v | Short, little-endian byte order w | BER-compressed integer\fnm X | Back up a byte x | Null byte Z | Same as ``a'', except that null is added with * or you can look for the FormatR package: http://formatr.sourceforge.net/ Hugh