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!

perl->Fortran: Translation of easy oneliner

Status
Not open for further replies.

nixwisser

Technical User
Sep 13, 2012
2
DE
Could anyone help me (dunnoanythingaboutfortran'nverylittleaboutperl) translating this recursive perl-lines into fortran?

$u = "abc";
while($u ~~ m/abc/)
{
$u =~ s/abc/abc bcd cde/
}

These lines replace within the variable "$u" the letters "abc" ("...s/abc/...") by "abc bcd cde" as long as $u contains "abc" ("while ...").

Why I need the translation and why I do not figure it out myself, as it obviously seems to be an easy practice task? Becaaaaause I am not a good programmer. In fact I am not even a bad programmer. I am one of these guys to testandtestandtestanddebugandtest until it works. I would need some fortran interpreter or compiler to translate the code. But that would be an overkill...

... sooooooooo: Could anyone help me?

nixwisser
 
Hi nixwisser,

Fortran doesn't have regular expressions like Perl. But in your case you only want to replace one substring with another.
I adapted your script so it will run only to the given maximum number of iterations:

nixwisser.pl
Code:
[COLOR=#008080]$u[/color] = [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]abc[/color][COLOR=#ff00ff]"[/color];
[COLOR=#008080]$max_iter[/color] = [COLOR=#ff00ff]5[/color];

[COLOR=#008080]$num[/color] = [COLOR=#ff00ff]0[/color];
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$num[/color][COLOR=#ff00ff]: [/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]u = [/color][COLOR=#008080]$u[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];

[COLOR=#804040][b]while[/b][/color] ([COLOR=#008080]$u[/color] =~ [COLOR=#804040][b]m/[/b][/color][COLOR=#ff00ff]abc[/color][COLOR=#804040][b]/[/b][/color]) {
  [COLOR=#008080]$num[/color]++;
  [COLOR=#008080]$u[/color] =~ [COLOR=#804040][b]s/[/b][/color][COLOR=#ff00ff]abc[/color][COLOR=#804040][b]/[/b][/color][COLOR=#ff00ff]abc bcd cde[/color][COLOR=#804040][b]/[/b][/color];
  [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#008080]$num[/color][COLOR=#ff00ff]: [/color][COLOR=#6a5acd]\$[/color][COLOR=#ff00ff]u = [/color][COLOR=#008080]$u[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];

  [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]$num[/color] >= [COLOR=#008080]$max_iter[/color]) {
    [COLOR=#804040][b]last[/b][/color]; [COLOR=#0000ff]# end loop[/color]
  }
}

Running the script above gives the following output:
Code:
$ perl nixwisser.pl
0: $u = abc
1: $u = abc bcd cde
2: $u = abc bcd cde bcd cde
3: $u = abc bcd cde bcd cde bcd cde
4: $u = abc bcd cde bcd cde bcd cde bcd cde
5: $u = abc bcd cde bcd cde bcd cde bcd cde bcd cde

To program the same thing in Fortran is a little bit longer
nixwisser.f95
Code:
[COLOR=#a020f0]program[/color] nixwisser
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]

  [COLOR=#0000ff]! substring str_old will be replaced with str_new[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]03[/color]), [COLOR=#2e8b57][b]parameter[/b][/color] :: str_old [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'abc'[/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]11[/color]), [COLOR=#2e8b57][b]parameter[/b][/color] :: str_new [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'abc bcd cde'[/color]
  [COLOR=#0000ff]! maximum iterations[/color]
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]parameter[/b][/color] :: max_iter [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]5[/color]

  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]1000[/color]) :: u
  [COLOR=#2e8b57][b]integer[/b][/color] :: idx, num

  [COLOR=#0000ff]! initial string[/color]
  u [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]' abc'[/color]
  num [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]

  [COLOR=#0000ff]! replacing in loop[/color]
  [COLOR=#804040][b]do[/b][/color]
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) num, [COLOR=#ff00ff]': u = "'[/color], [COLOR=#008080]trim[/color](u), [COLOR=#ff00ff]'"'[/color]

    idx [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]index[/color](u, [COLOR=#008080]trim[/color](str_old))

    [COLOR=#0000ff]! if nothing found or maximum number of iterations exceeded [/color]
    [COLOR=#804040][b]if[/b][/color] ((idx [COLOR=#804040][b]==[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b].or.[/b][/color] (num [COLOR=#804040][b]>=[/b][/color] max_iter)) [COLOR=#804040][b]then[/b][/color]
      [COLOR=#804040][b]exit[/b][/color] [COLOR=#0000ff]! end loop[/color]
    [COLOR=#804040][b]end if[/b][/color]

    u [COLOR=#804040][b]=[/b][/color] u([COLOR=#ff00ff]1[/color]:idx[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]//[/b][/color] str_new [COLOR=#804040][b]//[/b][/color] u(idx[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]3[/color]:)

    num [COLOR=#804040][b]=[/b][/color] num [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
  [COLOR=#804040][b]end do[/b][/color]
[COLOR=#a020f0]end program[/color] nixwisser

Here is the output:
Code:
$ g95 nixwisser.f95 -o nixwisser

$ nixwisser
 0 : u = " abc"
 1 : u = " abc bcd cde"
 2 : u = " abc bcd cde bcd cde"
 3 : u = " abc bcd cde bcd cde bcd cde"
 4 : u = " abc bcd cde bcd cde bcd cde bcd cde"
 5 : u = " abc bcd cde bcd cde bcd cde bcd cde bcd cde"
 
I have always known that fortran programmers have no sense of humor ;)

Perl does that as a oneliner that fits on a tshirt! And with fortran you have to do all that formal programming stuff! Now I know why my father in law (old skul fortran programmer) does not take my efforts to learn perl serious.

Well, mikrom, your translation (more a transition?) of the code was the polite and long way of sending me back to the kids to play. Thank you anyway.
 
nixwisser said:
Perl does that as a oneliner that fits on a tshirt! And with fortran you have to do all that formal programming stuff!
Ok it seems so, but it doesn't mean that Perl is better then Fortran :)
Both languages are complete different and were designed in different time for very different purposes. Fortran is one of oldest programming languages, developed in 1950s for scientific computing and it's compiled language. Perl was developed 30 years later, i.e. in 1980s mainly for text processing and it's interpreted language. In other words Fortran is very good for scientific computing and Pelr is very good for text processing.
 
Diagnosis: Perl programmer ;)
Incurable...
Fatal...
Fortunately, not contagious ;)
 
Diagnosis: Perl programmer ;)
Incurable...
Fatal...
Fortunately, not contagious ;)

What about eyesight or counting abilities ? ;-)

Somehow I count nixwisser's Perl structure to have five lines, but my judgement might be biased.

And if you strip down mikrom's prog to its bare essentials and leave aside all the smoothies, it hardly needs any more than this:

Code:
    character(1000) :: u
  u = ' abc'
  do
    idx = index(u, 'abc')
    if (idx .gt. 0)    u = u(1:idx-1) // 'abc bcd cde'// u(idx+3:)
  end do
  end


This has 7 lines together with the necessary type declaration and program termination, the working section has five lines. I do not know if Perl requires such things as well, if so, they are missing in nixwisser's code and would add a few lines. Of course, this prog runs as long as u can hold the characters and will abort with an error once an overflow occurs and it has no output - same with the Perl code as far as I can see.

Norbert

The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Yet another remarks:
- How about So we can get PCRE in one-line Fortran call...
- Well, let's code flow about a triangular wing for large values of M problem in Perl. Arm yourself with patience...
What's a funny (and irrelevant) echo of language wars...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top