Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting a character

Status
Not open for further replies.

ehsansharei

Programmer
Nov 5, 2013
4
DE
Hello Everybody,
I have short question and I hope the answer is easy and short. I have read a character in from a text file which looks like:

'__________NUMBER_OF_ELEMENTS_IS___________________________________270'

(instead of underlines I have empty spaces). I simply want '270' out of it as an integer.
I use FORTRAN 77 and I am new to it. Any answer is really appreciated.
Regards,
Ehsan
 
Is the position of the 270 always fixed, eg does it always start at column 70.

Are the number of words in the sentence fixed? Is it always 4 words?
 
if the number is on the end of line you can try something like this
Code:
[COLOR=#a020f0]program[/color] ehsansharei
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]80[/color]) line
  [COLOR=#2e8b57][b]integer[/b][/color]       line_length, num_elements

  line [COLOR=#804040][b]=[/b][/color] [COLOR=#804040][b]&[/b][/color]  
  [COLOR=#ff00ff]'          NUMBER OF ELEMENTS IS                                   270'[/color]
  
  line_length [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]len_trim[/color](line)
  [COLOR=#804040][b]read[/b][/color] (line(line_length[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]2[/color]:),[COLOR=#ff00ff]'(I3)'[/color]) num_elements

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'line = "'[/color], line, [COLOR=#ff00ff]'"'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'length of line = '[/color], line_length
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'num_elements = '[/color], num_elements 
[COLOR=#a020f0]end program[/color] ehsansharei

Output:
Code:
$ g77 ehsansharei.f -o ehsansharei --free-form

$ ehsansharei
 line = "
           NUMBER OF ELEMENTS IS                                   270           
 "
 length of line =  69
 num_elements =  270
 
Dear xwb,
Thanks for your answer. yes I have just these 4 words in the sentence fixed. But instead of 270 could be e.g. 50 and it could change the column position (not sure).
 
Dear Mikrom,
thanks for your answer. But I think len_trim belongs to fortran 90 and I have to use fortran 77.
Regards,
Ehsan
 
ehsansharei said:
...But I think len_trim belongs to fortran 90 and I have to use fortran 77...
I compiled it with g77 which is the Fortran 77 compiler, so I thought that it is in the standard.
 
Here
I found that INDEX() and LEN() should be in standard Fortran 77.
So you could try something like this using the 2 functions mentioned above:
Code:
[COLOR=#a020f0]program[/color] ehsansharei
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]50[/color]) line, label
  [COLOR=#2e8b57][b]integer[/b][/color]       num_elements

  line [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]" NUMBER OF ELEMENTS IS    270   "[/color]
  label [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'NUMBER OF ELEMENTS IS'[/color]

  [COLOR=#008080]call[/color] extract_number(line, [COLOR=#ff00ff]'NUMBER OF ELEMENTS IS'[/color], num_elements)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'line = "'[/color], line, [COLOR=#ff00ff]'"'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'number extracted = '[/color], num_elements 
[COLOR=#a020f0]end program[/color] ehsansharei

[COLOR=#a020f0]subroutine[/color] extract_number(line, label, num)
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#0000ff]! extracts NUM from string "   LABEL   NUM "[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]) line, label
  [COLOR=#2e8b57][b]integer[/b][/color] num
  [COLOR=#2e8b57][b]integer[/b][/color] from_position

  from_position [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]index[/color](line, label) [COLOR=#804040][b]+[/b][/color] [COLOR=#008080]len[/color](label)
  [COLOR=#804040][b]read[/b][/color] (line(from_position:), [COLOR=#804040][b]*[/b][/color]) num
[COLOR=#a020f0]end subroutine[/color] extract_number
Output:
Code:
$ g77 ehsansharei.f -o ehsansharei --free-form

$ ehsansharei
 line = " NUMBER OF ELEMENTS IS    270                     "
 number extracted =  270
Maybe your F77 compiler doesn't support free-form just like my g77. So you have to transform the code to fixed-form.
 
Thank you so much Mikrom! I think that should work now.
Regards,
Ehsan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top