Text Data Processing
Delimited Field
Pick a Column
For small fixed examples, index and slices can split a delimited line without adding a parser dependency.
Program
Play the program to choose which comma-separated field is copied into field.
delimited_field.f90
Replay: real traced execution (multi-file project)
program delimited_field_demo
implicit none
integer :: field_no
integer :: first_comma, second_comma
character(len=20) :: line
character(len=8) :: field
field_no = 2
line = 'red,green,blue'
first_comma = index(line, ',')
second_comma = first_comma + index(line(first_comma + 1:), ',')
select case (field_no)
case (1)
field = line(1:first_comma - 1)
case (2)
field = line(first_comma + 1:second_comma - 1)
case default
field = line(second_comma + 1:len_trim(line))
end select
print '(A)', trim(field)
end program delimited_field_demo
program delimited_field_demo
implicit none
integer :: field_no
integer :: first_comma, second_comma
character(len=20) :: line
character(len=8) :: field
field_no = 1
line = 'red,green,blue'
first_comma = index(line, ',')
second_comma = first_comma + index(line(first_comma + 1:), ',')
select case (field_no)
case (1)
field = line(1:first_comma - 1)
case (2)
field = line(first_comma + 1:second_comma - 1)
case default
field = line(second_comma + 1:len_trim(line))
end select
print '(A)', trim(field)
end program delimited_field_demo
program delimited_field_demo
implicit none
integer :: field_no
integer :: first_comma, second_comma
character(len=20) :: line
character(len=8) :: field
field_no = 3
line = 'red,green,blue'
first_comma = index(line, ',')
second_comma = first_comma + index(line(first_comma + 1:), ',')
select case (field_no)
case (1)
field = line(1:first_comma - 1)
case (2)
field = line(first_comma + 1:second_comma - 1)
case default
field = line(second_comma + 1:len_trim(line))
end select
print '(A)', trim(field)
end program delimited_field_demo
field_no ← 2
8field_no = 29line = 'red,green,blue'values this step2field_noline ← red,green,blue
8field_no = 29line = 'red,green,blue'10first_comma = index(line, ',')values this stepred,green,bluelinefirst_comma ← 4
9line = 'red,green,blue'10first_comma = index(line, ',')11second_comma = first_comma + index(line(first_comma + 1:), ',')values this step4first_commared,green,bluelinesecond_comma ← 10
10first_comma = index(line, ',')11second_comma = first_comma + index(line(first_comma + 1:), ',')12select case (field_no)values this step10second_comma4first_commamatched ← case (2)
11second_comma = first_comma + index(line(first_comma + 1:), ',')12select case (field_no)13 case (1)values this stepcase (2)matched2field_nofield ← green
15case (2)16 field = line(first_comma + 1:second_comma - 1)17case defaultvalues this stepgreenfieldprint '(A)', trim(field)
19 end select20 print '(A)', trim(field)21end program delimited_field_demooutputgreenvalues this stepgreenfield
field_no ← 1
8field_no = 19line = 'red,green,blue'values this step1field_noline ← red,green,blue
8field_no = 19line = 'red,green,blue'10first_comma = index(line, ',')values this stepred,green,bluelinefirst_comma ← 4
9line = 'red,green,blue'10first_comma = index(line, ',')11second_comma = first_comma + index(line(first_comma + 1:), ',')values this step4first_commared,green,bluelinesecond_comma ← 10
10first_comma = index(line, ',')11second_comma = first_comma + index(line(first_comma + 1:), ',')12select case (field_no)values this step10second_comma4first_commamatched ← case (1)
11second_comma = first_comma + index(line(first_comma + 1:), ',')12select case (field_no)13 case (1)values this stepcase (1)matched1field_nofield ← red
13case (1)14 field = line(1:first_comma - 1)15case (2)values this stepredfieldprint '(A)', trim(field)
19 end select20 print '(A)', trim(field)21end program delimited_field_demooutputredvalues this stepredfield
field_no ← 3
8field_no = 39line = 'red,green,blue'values this step3field_noline ← red,green,blue
8field_no = 39line = 'red,green,blue'10first_comma = index(line, ',')values this stepred,green,bluelinefirst_comma ← 4
9line = 'red,green,blue'10first_comma = index(line, ',')11second_comma = first_comma + index(line(first_comma + 1:), ',')values this step4first_commared,green,bluelinesecond_comma ← 10
10first_comma = index(line, ',')11second_comma = first_comma + index(line(first_comma + 1:), ',')12select case (field_no)values this step10second_comma4first_commamatched ← case default
11second_comma = first_comma + index(line(first_comma + 1:), ',')12select case (field_no)13 case (1)values this stepcase defaultmatched3field_nofield ← blue
17 case default18 field = line(second_comma + 1:len_trim(line))19end selectvalues this stepbluefieldprint '(A)', trim(field)
19 end select20 print '(A)', trim(field)21end program delimited_field_demooutputbluevalues this stepbluefield
index
`index(line, ',')` finds the position of a delimiter.
slice
`line(first:last)` extracts a character section.
select case
`select case` chooses the field-copy rule for this small example.