Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1993
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1993

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: I want to read numbers written in E-format.

  • To: mathgroup at yoda.physics.unc.edu
  • Subject: Re: I want to read numbers written in E-format.
  • From: "Tyler Perkins" <perkins at spot.colorado.edu>
  • Date: Mon, 20 Dec 93 11:56:07 MST

Peter Whaite wrote:
>I have programs that write Mma expressions to a file.  I bring this file into
>Mma using Get.  Unfortunately there are numbers in the file which are
>sometimes in e-format, so e.g. 1.234e+456 gets  parsed as 456 + 1.234*e.
>ReadList can handle e-format numbers but I can't use it.

You didn't say what kind of system you're using, but if it's a Unix machine,
you can use the following sh script I wrote for just this purpose.  Using
printf(), I find it convenient to have my C programs output data in
Mathematica format. E.g.:

    y = { 0.1, 2.345e7, -8.9e-11 }

After executing "sci2mma filename" from the shell, I can just use << to Get
the data into Mathematica.

My script uses the utility "mktemp".  In case your system doesn't have
mktemp, I've also included a version I wrote which will suffice.

Have fun!
    Tyler Perkins                       perkins at spot.colorado.edu
    Boulder, CO

- - - - - - - - - - - - - - - - - - cut here - - - - - - - - - - - - - - - -
#!/bin/sh

# NAME
#   sci2mma - Scientific to Mathematica Format converter
#
# SYNOPSIS
#   sci2mma [ -nobuffer ] [ file ] ...
#
# DESCRIPTION
#   sci2mma acts as a filter or file conversion utility to convert
#   occurrences of strings of the form "1.234e-5" to "1.234*10^-5 .
#   Thus a text file produced by a program written in C, Fortran,
#   etc., may be used as input to Mathematica using its Get[..]
#   function.
#
#   When called with no  filename arguments, sci2mma takes the
#   standard input as input and sends its output to the standard
#   output.  If called with filename arguments, each file is
#   modified as described above.
#
#   The option -nobuffer may be used when there are no filename
#    arguments to ensure that sci2mma immediately processes each
#   line as it is received from standard input, sending the
#   resulting line immediately to standard output.  This is much
#   slower than not using the -nobuffer option.

sedcmd='s/\([0-9\.]\)[eE]\([0-9\+\-]\)/\1*10^\2/g'

useBuffer=true
if [ "$1" = "-nobuffer" ]
then    shift
        useBuffer=false
fi

if [ "$*" = "" ]
then
    if $useBuffer
    then sed $sedcmd
    else

#  Read a line, then make substitution.  We do this one line at a
#  time to ensure that sci2mma sends a line of output immediately
#  after a line of input.  This is much slower because of the need
#  to start up and exit a sed process for each line of input.

        IFS=''
        while read aLine
        do  echo $aLine | sed $sedcmd
        done
    fi

else temp=`mktemp`
    for f in $*
    do  sed $sedcmd $f >$temp
        cp $temp $f
    done
    rm $temp
fi

- - - - - - - - - - - - - - - - - - cut here - - - - - - - - - - - - - - - -
#!/bin/sh

#                               mktemp
#
#   Make a temporary filename.  This utility attempts to duplicate the
#   function of mktemp as found on other systems such as Ultrix.

dir="/tmp"
prfx="$USER"
create=false

while [ "$1" != "" ]
do case $1 in
    -d) shift
        dir="`echo $1 | sed 's!\(.*\)/$!\1!'`"
        shift
        continue
        ;;
    -p) shift
        prfx=$1
        shift
        continue
        ;;
    -c) shift
        create=true
        continue
        ;;
     *) echo 'mktemp usage: mktemp [-d directory] [-p prefix] [-c]'
        exit 1
        ;;
    esac
done

name="$dir/$prfx.$$`date | sed 's/.*:\(..\):\(..\).*/\1\2/'`"

# If file with name $name already exists, recurse until not.
if [ -f $name  -o  -d $name ]
then name="`mktemp`"
fi

if $create
then >$name
else echo $name
fi

exit 0





  • Prev by Date: Re: I want to read numbers written in E-format.
  • Next by Date: Re: Rotations.m package bug?
  • Previous by thread: Re: I want to read numbers written in E-format.
  • Next by thread: New math.el