• Support PF! Buy your school textbooks, materials and every day products Here!

Counting characters in fortran

  • #1
7
0
< Mentor Note -- thread moved to HH from the technical forums, so no HH Template is shown >

I have a file Aggregation.dat
########################################################################################
6.999e+04 4.50000e+01 2.22222e+00 4.94813e+00 2.50000e+01 1.00000e+00
7.004e+04 4.40000e+01 2.27273e+00 4.99711e+00 2.50000e+01 1.00000e+00
7.009e+04 4.30000e+01 2.32558e+00 5.04732e+00 2.50000e+01 1.00000e+00
MAX 25 MIN 1 AVG 2.325581 STD 5.047315 AGG_NUM 43 AGGREGATES
{ 0 } { 6 } { 7 } { 8 } { 9 } { 14 } { 63 57 35 70 62 3 31 32 1 24 64 10 39 94 5 68 74 54 2 40 12 28 27 17 } { 21 } { 30 } { 33 } { 34 } { 36 } { 37 } { 41 } { 43 } { 46 } { 52 48 } { 49 } { 50 } { 51 } { 56 } { 59 } { 61 } { 66 } { 69 } { 73 } { 20 91 60 4 71 19 13 58 53 80 25 76 47 93 29 16 45 38 22 92 72 18 65 11 75 } { 77 } { 78 } { 79 } { 81 } { 84 } { 85 } { 86 } { 87 } { 88 } { 89 } { 90 } { 83 15 67 42 82 44 55 26 95 } { 96 } { 97 } { 23 98 } { 99 }
#######################################################################################

I would like to count only the numbers that are only between "{ }", does somebody have an idea of how to do in fortran?

Please help me.
 
Last edited by a moderator:

Answers and Replies

  • #2
11,491
5,026
Welcome to PF!

Is this a homework assignment?

What is your experience with Fortran?

Would some other language like Awk, Perl, Python or Ruby be more suited to the problem?
 
  • #3
7
0
Welcome to PF!

Is this a homework assignment?

What is your experience with Fortran?

Would some other language like Awk, Perl, Python or Ruby be more suited to the problem?

I am doing research for my MSc with the assistance of simulations, but since I use scripts to some calculations in fortran, for this my option in fortran,
Excuse my writing, I do not speak English.
 
  • #4
34,053
9,912
Do you want to count characters (topic), numbers (first post), digits, or something else?

Take { 23 98 } for example:
1 because it is one bracket pair?
2 because there are two numbers?
4 because there are 4 numeric characters (digits)?
7 because there are 7 characters between the brackets?

Worst case: loop over the whole file, check digit by digit where you are, keep track of what you want to count. I don't know Fortran, but that solution will work in every programming language and does not require more specialized features.
 
  • #5
berkeman
Mentor
56,841
6,820
I would like to count only the numbers that are only between "{ }", does somebody have an idea of how to do in fortran?
How would you do it in C? Or in Tcl/Tk? Do you know any other programming languages?

Have you done any character file scanning up to now in your learning of FORTRAN?
 
  • #6
7
0
Do you want to count characters (topic), numbers (first post), digits, or something else?

Take { 23 98 } for example:
1 because it is one bracket pair?
2 because there are two numbers?
4 because there are 4 numeric characters (digits)?
7 because there are 7 characters between the brackets?

Worst case: loop over the whole file, check digit by digit where you are, keep track of what you want to count. I don't know Fortran, but that solution will work in every programming language and does not require more specialized features.


Each number is related to a molecule. If you have more than one number between "{}" means that it is an aggregate, Ex .: { 28 31 5 }.
If there is only one number in "{}", this molecule is free, Ex .: {19}. I want to count only the free molecules.
 
  • #7
7
0
How would you do it in C? Or in Tcl/Tk? Do you know any other programming languages?

Have you done any character file scanning up to now in your learning of FORTRAN?


I use tcl / tk. To this objective, I have not done any script. But I have done for other objectives.
 
  • #8
1,065
53
Hhhmmm...I copied and pasted the input data from above, it looks like this:
Code:
6.999e+04 4.50000e+01 2.22222e+00 4.94813e+00 2.50000e+01 1.00000e+00
7.004e+04 4.40000e+01 2.27273e+00 4.99711e+00 2.50000e+01 1.00000e+00
7.009e+04 4.30000e+01 2.32558e+00 5.04732e+00 2.50000e+01 1.00000e+00
MAX 25 MIN 1 AVG 2.325581 STD 5.047315 AGG_NUM 43 AGGREGATES { 0 } { 6 } { 7 } ...
meaning, the aggregates are found in the forth line of the input file; so, I read and ignore the first 3 lines.

Take the following code and put it in a file named, say, nagg.f90
Code:
program nagg
  character(1000) line
  integer i, j, k, first, last

  read(*,*) line
  read(*,*) line
  read(*,*) line
  read(*,'(A1000)') line    
  first = index(line, "{")
  last  = index(line, "}", .true.)
  write(*,*) "Aggregates:"
  i = first - 1
  do
    j = (i-1) + ( index(line(i:1000), "{") + 1 )
    k = (i-1) + ( index(line(i:1000), "}") - 1 )
    if (index(trim(adjustl(line(j:k)))," ") == 0) write(*,*) line(j:k)
    if (k+1 == last) exit
    i = k + 2
  end do    
end program nagg
Compile like this:
Code:
gfortran -o nagg nagg.f90
Run like this:
Code:
nagg < Aggregation.dat
It should output the following:
Code:
 Aggregates:
  0 
  6 
  7 
  8 
  9 
  14 
  21 
  30 
  33 
  34 
  36 
  37 
  41 
  43 
  46 
  49 
  50 
  51 
  56 
  59 
  61 
  66 
  69 
  73 
  77 
  78 
  79 
  81 
  84 
  85 
  86 
  87 
  88 
  89 
  90 
  96 
  97 
  99
 
  • #9
7
0
Hhhmmm...I copied and pasted the input data from above, it looks like this:
Code:
6.999e+04 4.50000e+01 2.22222e+00 4.94813e+00 2.50000e+01 1.00000e+00
7.004e+04 4.40000e+01 2.27273e+00 4.99711e+00 2.50000e+01 1.00000e+00
7.009e+04 4.30000e+01 2.32558e+00 5.04732e+00 2.50000e+01 1.00000e+00
MAX 25 MIN 1 AVG 2.325581 STD 5.047315 AGG_NUM 43 AGGREGATES { 0 } { 6 } { 7 } ...
meaning, the aggregates are found in the forth line of the input file; so, I read and ignore the first 3 lines.

Take the following code and put it in a file named, say, nagg.f90
Code:
program nagg
  character(1000) line
  integer i, j, k, first, last

  read(*,*) line
  read(*,*) line
  read(*,*) line
  read(*,'(A1000)') line 
  first = index(line, "{")
  last  = index(line, "}", .true.)
  write(*,*) "Aggregates:"
  i = first - 1
  do
    j = (i-1) + ( index(line(i:1000), "{") + 1 )
    k = (i-1) + ( index(line(i:1000), "}") - 1 )
    if (index(trim(adjustl(line(j:k)))," ") == 0) write(*,*) line(j:k)
    if (k+1 == last) exit
    i = k + 2
  end do 
end program nagg
Compile like this:
Code:
gfortran -o nagg nagg.f90
Run like this:
Code:
nagg < Aggregation.dat
It should output the following:
Code:
Aggregates:
  0
  6
  7
  8
  9
  14
  21
  30
  33
  34
  36
  37
  41
  43
  46
  49
  50
  51
  56
  59
  61
  66
  69
  73
  77
  78
  79
  81
  84
  85
  86
  87
  88
  89
  90
  96
  97
  99





I am very grateful for the help. But in fact my input file is much higher, I just put the final. Instead of ignoring just three lines, we would have to ignore 1002 lines in this case. And if this number of lines change occasionally because they depend on the number of steps from my simulation?

Up to this point the script worked just fine, thank you.
 
Last edited:
  • #10
33,270
4,975
I am very grateful for the help. But in fact my input file is much higher, I just put the final. Instead of ignoring just three lines, we would have to ignore 1002 lines in this case. And if this number of lines change occasionally because they depend on the number of steps from my simulation?
There are a couple of simple changes that you can make to the code posted by @gsal:
1. If the number of lines to ignore changes often, take input from the user on how many lines to skip over and run a counting loop (DO ... NEXT) to do just that.
2. If the number of lines to ignore changes infrequently, define a parameter that specifies the number of lines to skip over, again using a counting loop. The disadvantage of this approach is that if the parameter needs to be changed, you need to recompile the code, but if that doesn't happen often, that's not too big a price to pay.
 
  • #11
34,053
9,912
If you are interested in the first line with brackets, look for brackets and ignore the lines until you find one with brackets. Or look for "AGGREGATES" if that is always in front of the aggregates.
 
  • #12
7
0
If you are interested in the first line with brackets, look for brackets and ignore the lines until you find one with brackets. Or look for "AGGREGATES" if that is always in front of the aggregates.

good idea.
 
  • #13
7
0
There are a couple of simple changes that you can make to the code posted by @gsal:
1. If the number of lines to ignore changes often, take input from the user on how many lines to skip over and run a counting loop (DO ... NEXT) to do just that.
2. If the number of lines to ignore changes infrequently, define a parameter that specifies the number of lines to skip over, again using a counting loop. The disadvantage of this approach is that if the parameter needs to be changed, you need to recompile the code, but if that doesn't happen often, that's not too big a price to pay.


OK thanks.
 

Related Threads on Counting characters in fortran

Replies
1
Views
940
Replies
7
Views
3K
Replies
1
Views
1K
Replies
0
Views
966
Replies
0
Views
1K
  • Last Post
Replies
10
Views
2K
  • Last Post
Replies
1
Views
2K
Replies
8
Views
3K
Top