0

i was trying to do something like this,

i will be given 5 numbers in 5 lines and my program should take only the ith integer from the ith line.

1  2 3 4 5
5 6   7 8 9
10 11   11   13 14
100     2000 300 2 1
7 77 777 7777      77777

so 1 6 11 2 77777 should be taken as input to a array sized 5

how to do it properly with loop? (as you can see, the width of the integer and space between them may vary )

thank you.

upd: i want to do this with a loop, so that i don't have to type it manually. Also i know it can be done with another array, but as i am learning Format so i am curious to know if it is possible to do this with formatting.

program selective
    implicit none
    integer a(5)
    integer ignore, i
    read(*,*) a(1), ignore, ignore, ignore, ignore
    read(*,*) ignore, a(2), ignore, ignore, ignore
    read(*,*) ignore, ignore, a(3), ignore, ignore
    read(*,*) ignore, ignore, ignore, a(4), ignore
    read(*,*) ignore, ignore, ignore, ignore, a(5)
    write(*,*) (a(i), i=1,5)
end program selective
Ardhendu
  • 1
  • 2
  • In the linked question you'll find the basics of how to "skip" columns in your input. Which column you want to read varies on each line, but it's a simple extension. If you have specific problems doing that, then (as in a previous comment) show your attempt for context. – francescalus Sep 04 '20 at 10:27
  • @HighPerformanceMark , i am sorry, i was completely out of clue how to do it with formatting, so i couldn't provide any code. pardon me please. – Ardhendu Sep 04 '20 at 10:55
  • @francescalus i've edited my question. so to be specific, i want to do it with formatting. – Ardhendu Sep 04 '20 at 10:56
  • Reading such uneven columns with an explicit format (rather than list-directed input) is a tricky, but entirely different problem. If you just want to read the diagonal then, in most cases, you really are better off using list-directed input. – francescalus Sep 04 '20 at 15:35
  • @francescalus than you very much for your feedback! should i create another question for this purpose? – Ardhendu Sep 04 '20 at 16:02
  • Another question would make sense, but first have a look at other questions on that topic (such as [this one](https://stackoverflow.com/q/21170031/3157076), [this one](https://stackoverflow.com/q/27846138/3157076) and [this one](https://stackoverflow.com/q/29572830/3157076). Essentially, you need to parse the lines manually (rather than hope to find a format which will "work"). A new question should look at difficulties you have in parsing rather than asking about a format. – francescalus Sep 04 '20 at 16:17
  • @francescalus i understand now, thank you very much for your kind co-operation :) – Ardhendu Sep 06 '20 at 06:30

0 Answers0