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!

problem with forall

Status
Not open for further replies.

mikibelavista

Technical User
Jan 18, 2012
32
I have array and I want to compare elements,how many 100.3 10.0 pairs do I have
100.3 10.0
110.0 10.0
100.3 10.0
I have tried this:
program pck

implicit none
real,dimension(10,2) :: a
integer,dimension(10) :: b
integer :: i,check

open(10,file='pic.txt')

do i=1,10
read(10,*)a(i,:)
end do

forall (i=1:10)
where (a(i,:) == a(i,:))
check=check+1
check=b(i)
end where
end forall
end program

But:
pck.f90:16.1:

check=check+1
1
Warning: The FORALL with index 'i' is not used on the left side of the assignment at (1) and so might cause multiple assignment to this object
pck.f90:17.1:

check=b(i)
1
Warning: The FORALL with index 'i' is not used on the left side of the assignment at (1) and so might cause multiple assignment to this object
pck.f90:16.1:

check=check+1
1
Error: WHERE assignment target at (1) has inconsistent shape
pck.f90:17.1:

check=b(i)
1
Error: WHERE assignment target at (1) has inconsistent shape
 
Hhhmmm...forall and where constructs are not for everything, you know? You should read up more carefully on how to use them, their usage is not as universal as the 'do' loop.

Were you asked to implement such logic using forall and where? or is that something you figure you would do; otherwise, it can be easily implemented:

Input file
Code:
100.3 10.0
110.0 10.0
100.3 10.0
110.0 10.0
100.3 10.0
100.3 10.0
Program
Code:
program pcount
    implicit none
    
    real, dimension(10,2) :: a
    integer, dimension(10) :: b
    
    integer :: i, n, check
    real :: r1, r2
        
    open(10, file='pairs.in')    ! input file
    
    n = 6                       ! numbe of lines to read from file
    r1 = 100.3  ; r2 = 10.0     ! pair to look for
    
    do i = 1, n                 ! read data
        read(10,*) a(i,:)
    end do
    write(*,'(f6.1,f6.1)') (a(i,:),i=1,n)   ! confirm data
    
    check = 0
    do i=1,n
        if ( a(i,1)==r1 .and. a(i,2)==r2 ) then
            b(i) = 1
        end if
    end do
    check = sum(b)
    write(*,*) 'number of pairs = ', check
        
end program pcount
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top