Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Transfering string sequence to integer array

Vasili_M_
Beginner
1,059 Views

I want to be able to transter a sequence of numbers in a string to an array.

My question is what will be the most intuitive way to behave when the number

of elements in s are different from size of isq 

 

Integer, Allocatable :: isq(:)
Call split ( "2/3/5", "/", isq ) 

subroutine split (s,delim,isq)
  Character (len=*) :: s
  Character (len=1) :: delim
  Integer :: isq  

end subroutine

 

0 Kudos
5 Replies
Vasili_M_
Beginner
1,059 Views

If size(isq) < nfields(s,delim) I fill array elements until size(isq) is reached, neglecting the rest.

When they are same size there is no problem.

So what remains is when size(isq) has more elements being passed.

 

0 Kudos
Vasili_M_
Beginner
1,059 Views

The best thing I found has been not to touch them, unless the user demands to repeat the last element of s. 

0 Kudos
FortranFan
Honored Contributor III
1,059 Views

You may want to consider starting from the basics - see Dr Fortran blog at https://software.intel.com/en-us/blogs/2013/12/30/doctor-fortran-in-its-a-modern-fortran-world.  The books mentioned in this blog can give you a sound footing in the fundamentals.

Back to your example, you can allocate the integer array within your procedure which can then make your question moot.

Integer, Allocatable :: isq(:)
Call split ( "2/3/5", "/", isq ) 

subroutine split (s,delim,isq)
  Character (len=*), intent(in) :: s
  Character (len=1), intent(in) :: delim
  Integer, allocatable, intent(out) :: isq(:)  

  ..

  allocate(isq(..), stat=..)
  ..
end subroutine

Consider the intent specifications for the dummy argument of procedure split: look up the details of intent(out) specification for dummy arguments with allocatable attribute.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,059 Views

FortranFan,

I think you intended to count the number of delim's then allocate isq(nDelims+1), then fill in the array.

Jim Dempsey

0 Kudos
FortranFan
Honored Contributor III
1,059 Views

jimdempseyatthecove wrote:

FortranFan,

I think you intended to count the number of delim's then allocate isq(nDelims+1), then fill in the array.

Jim Dempsey

Yes, you're right Jim.

0 Kudos
Reply