home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / fortran / 5184 < prev    next >
Encoding:
Text File  |  1993-01-26  |  6.1 KB  |  108 lines

  1. Newsgroups: comp.lang.fortran
  2. Path: sparky!uunet!paladin.american.edu!howland.reston.ans.net!usc!sdd.hp.com!crash!photon.photon.com!wmc
  3. From: wmc@photon.com (Bill Cornette)
  4. Subject: Re: Reading a real from a character substring
  5. Message-ID: <1993Jan25.214937.4491@photon.com>
  6. Sender: usenet@photon.com (Usenet dummy users)
  7. Organization: Photon Research Associates, Inc.
  8. X-Newsreader: TIN [version 1.1 PL6]
  9. References: <1993Jan19.080949.17968@syma.sussex.ac.uk>
  10. Date: Mon, 25 Jan 1993 21:49:37 GMT
  11. Lines: 95
  12.  
  13. Conor McMenamin (mppu3@syma.sussex.ac.uk) wrote:
  14. : Conor McMenamin (me) wrote:
  15. : : Hi,
  16. : :     I need to find as standard as possible a routine to read a real
  17. : : number from a character variable (well actually a substring of a
  18. : : character variable but that's easy to work round...). However my problem
  19. : : is that I do not know the format of the number which I am reading in. I
  20. : : have tried to "READ (string(a:b),*) real" but my compiler tells me that
  21. : : internal I/O must be formatted....anybody got any ideas how to do this a
  22. : : standard way? Or is my compiler broken?
  23. : : 
  24. : :         Cheers,
  25. : :         Conor
  26. : Thanks for the multitude of replies I recieved. All were accuarte as far
  27. : as I could make out, but the winner (for exactly solving my problem in
  28. : minimum code lines) is Paul Schlyter who wrote:
  29. : Paul>Try:
  30. : Paul>
  31. : Paul>       character fmt(6)
  32. : Paul> 
  33. : Paul> ........
  34. : Paul>       write( fmt, '(''(F'',I1,''.0)'')' ) b-a+1
  35. : Paul>       read( string(a:b), fmt ) real
  36. : Paul> 
  37. : Paul> Format strings can be created at runtime.....
  38. : Paul> Note that the .0 in the created F format only matters at output,
  39. : Paul> not at input.
  40. : Paul>
  41.  
  42.     If you want to have the format calculated try the FUNCTION:
  43.  
  44.  
  45.       REAL FUNCTION GETVAR(VARIAB)                                      gva 0010
  46. C     Created on:  Wed Nov 18 15:56:43 1992                             gva 0020
  47. C     Created by:  Dr. William M. Cornette                              gva 0030
  48. C                                                                       gva 0040
  49. C**** This FUNCTION reads a REAL variable contained in free             gva 0050
  50. C       format in the CHARACTER string VARIAB.                          gva 0060
  51. C                                                                       gva 0070
  52. C VARIAB - CHARACTER*(*) Variable - Input string                        gva 0080
  53. C                                                                       gva 0090
  54. C**** INTRINSIC and EXTERNAL Declarations                               gva 0100
  55. C                                                                       gva 0110
  56.       INTRINSIC        LEN                                              gva 0120
  57. C                                                                       gva 0130
  58. C**** Argument Declarations                                             gva 0140
  59. C                                                                       gva 0150
  60.       CHARACTER*(*)    VARIAB                                           gva 0160
  61. C                                                                       gva 0170
  62. C**** Local Variable Declarations                                       gva 0180
  63. C                                                                       gva 0190
  64.       INTEGER        I,ICNT,LENSTR,IOS                                  gva 0200
  65.       REAL           DEFALT                                             gva 0210
  66.       CHARACTER*7    FMTSTR                                             gva 0220
  67. C                                                                       gva 0230
  68.       DATA           DEFALT /-9999./                                    gva 0240
  69. C                                                                       gva 0250
  70.       LENSTR=LEN(VARIAB)                                                gva 0260
  71.       ICNT=1                                                            gva 0270
  72.       DO 101 I = LENSTR,1,-1                                            gva 0280
  73.          IF (VARIAB(I:I).NE.' ') THEN                                   gva 0290
  74.             ICNT=I                                                      gva 0300
  75.             GO TO 102                                                   gva 0310
  76.          END IF                                                         gva 0320
  77.  101  CONTINUE                                                          gva 0330
  78.  102  CONTINUE                                                          gva 0340
  79.       WRITE(UNIT=FMTSTR,FMT=1,ERR=104,IOSTAT=IOS) ICNT                  gva 0350
  80.       READ(UNIT=VARIAB(1:ICNT),FMT=FMTSTR,ERR=103,IOSTAT=IOS) GETVAR    gva 0360
  81.  103  CONTINUE                                                          gva 0370
  82.       IF (IOS.NE.0) GETVAR=DEFALT                                       gva 0380
  83. C                                                                       gva 0390
  84.       RETURN                                                            gva 0400
  85. C                                                                       gva 0410
  86.  104  CONTINUE                                                          gva 0420
  87.       WRITE(UNIT=*,FMT=2) ICNT,VARIAB(1:ICNT),IOS                       gva 0430
  88.       STOP 'Error No. 208'                                              gva 0440
  89. C                                                                       gva 0450
  90.     1 FORMAT('(G',I2,'.0)')                                             gva 0460
  91.     2 FORMAT(1X/1X,'WRITE Error in getvar.  Calculation terminated.'/   gva 0470
  92.      +4X,'ICNT   = ',I5,1X,A/                                           gva 0480
  93.      +4X,'IOSTAT = ',I5)                                                gva 0490
  94.       END                                                               gva 0500
  95.  
  96.  
  97.  
  98. ---------------------------------------------------------------------------
  99. ---------------------------------------------------------------------------
  100. William M. Cornette         Photon Research Associates, Inc.
  101. (619) 455-9741              10350 N. Torrey Pines Road, Suite 300
  102. FAX (619) 455-0658          La Jolla, California  92037
  103. wmc@photon.com
  104. ---------------------------------------------------------------------------
  105. T.A.N.S.T.A.A.F.L.
  106.