Contact US

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Find and replace in fortran

Find and replace in fortran

Find and replace in fortran

How to do find and replace in Fortran or ideas for a different way of solving the problem

I am reading in a column of asteroid diameters from a file, some of the asteroids are however not spherical but can be e.g. potato shaped, and then instead of a diameter I get something like "250x23x75.5" or if just two sides are known I get something like "250x23".

What I need to do with these values are to take the non-spherical ones and calculate the diameter of a sphere with equal volume.

My problem is that I don't know how to take a value like "diameter = 250x23x75.5" and get the individual numbers out so I can calculate anything.

I have been considering doing an "find and replace" of all the x to *, so I can directly use it in the calculations. But I am not sure how to do "find and replace", or if I will have problems using a string in a formula. And then how I can be sure to insert "250x23" into a different formula than "250x23x75.5".

For the find and replace I have considered turning each value into an array, so e.g. "250x23" would be the array (2,5,0,x,2,3) and then running a if loop on it, to search for X's and replace them. But I am not sure how to do this either, and it seems like a complicated way of doing something so simple.

I hope someone are willing to give me some new ideas.

RE: Find and replace in fortran

I have not used FORTRAN for many years now, but isn't this the sort of thing that the CHARACTER*1 variable type was introduced for.  Read the data as a CHARACTER string, then search that string for whatever signposts you are looking for.  When you need to extract the numbers from the substrings between the signposts, use an "internal read".

RE: Find and replace in fortran

It probably can be done in Fortran, but it looks like something that would be much more easily done in a spreadsheet to me.

Doug Jenkins
Interactive Design Services

RE: Find and replace in fortran

Sure, it can be done...you just need to be patient and get creative.

For example, you read the info as a string and then you inspect it to find out how many 'X' it has and at what index they are at...then you do an internal read of the characters in between.

Or, you could take a look at the modules put together in this page:

Or, you could use something else other than fortran that has a lot more abilities on manipulating strings and spliting, etc, ...like, say, Python.


RE: Find and replace in fortran

Thanks you so much for all your answers, I really appreciate that you used your time on answering me.

IDS: I took data into excel and it was much easier than Fortran, and I got some plots out after some manipulating, but my input file will get renewed often, so in the long run, it is better for me to do as much to in Fortran as possible.

Denial and gsal:Thanks for pointing me towards "internal read" and charecter*1 variables. (The reason why I am using Fortran is my boss wants me to build on his old programs to save time).

Xwb- You wrote a script for me, WOW!!! Thanks! I am going through it now, to understand it and make it fit in my program.     

RE: Find and replace in fortran

if it's possible, use python as a glue language and call your fortran routines from it

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login


Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a part’s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close