0

I've an integer array

int(4) :: idate            ! 1979 March 1st 00hrs

write(*,*)idate            ! prints ' 0 3 1 1979'

I want idate to be saved in a different variable (integer/integer array only) which will print the date as:

1979030100

without changing it into char/strings.

Can this be done. Pardon me if it is trivial but I've spent quite a bit of my time on it.

2
  • It's relatively easy to print the date in that form, do you need to also store it as an integer of that description ? Commented Sep 27, 2012 at 10:19
  • Yes, I need it to store it in a variable which will go as an input to other part of the code. I'll be obliged if you tell me how it can be done!!! Thanks Commented Sep 27, 2012 at 10:23

1 Answer 1

1

You could do something like this:

integer :: date_as_int
...
date_as_int = idate(1)*10**6 + idate(2)*10**4 + idate(3)*10**2 + idate(4)

You might even get away with

date_as_int = sum(idate*10**[6,4,2,0])

or

date_as_int = dot_product(idate, 10**[6,4,2,0])

The square brackets syntax is from Fortran 2003. With older compilers [6,4,2,0] should be replaced by (/6,4,2,0/).

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Mark! It seems ok for now but the problem will come when year and date (changes at runtime) will be single digit (the exponent of 10 should change accordingly). Ofcourse, this too can be handled by introducing if-else loops. Is there any more sophisticated way???
If you can explain precisely a more sophisticated way of achieving your ends SO may be able to help you to write your code. Right now, I can't see any need for more sophistication. And for sure, if your input data doesn't match the example in your question you'll have to modify the code; again, I can't see any problems ahead for you.
I am sorry Mark if you are hurt by my comment. I could print it using: write(*,'(i4,3i2.2)')idate(4),idate(2),idate(3),idate(1). In a similar way, I thought there would exist some function like TRIM or ADJUSTL for integers and the task could be done.
I'm not hurt, I'm thick-skinned. But I'm increasingly puzzled by what you are trying to do. You commented that you weren't trying to format the date for output but to assign it to an integer variable. Now you're asking questions about functions defined for character variables. If you need/want any more help, clarify, if not don't.
The second one should be either DOT_PRODUCT(idate, 10**[6,4,2,0]) or SUM(idate*10**[6,4,2,0]).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.