Is something like this (in Matlab) possible in Fortran?
integer, parameter, dimension(4) :: odd(1,3,5,7)
integer, parameter, dimension(4) :: even(2,4,6,8)
Or simply:
even_index = [2 4 6 8 10]
odd_index = [1 3 5 7 9]
And then index every second number in somearrays:
somearray(even_index) = somearray(even_index) * 2.0
somearray(odd_index) = somearray(odd_index) * 2.0
Is it possible? I get compiler error when I try it...
Regards,
Martin
> integer, parameter, dimension(4) :: odd(1,3,5,7)
> integer, parameter, dimension(4) :: even(2,4,6,8)
This is almost valid, but doesn't mean what you think. The (1,3,5,7) is
not an array of values. It is the dimensions, overriding the
dimension(4). And you don't specify a value at all, which is what makes
it invalid. What you want is more like
integer, parameter, dimension(4) :: odd = [1,3,5,7]
where I'm using the f2003 square bracket array constructor syntax. If
your compiler doesn't accept that, substitute (/1,3,5,7/), but do note
that you cannot use just (1,3,5,7).
> even_index = [2 4 6 8 10]
> odd_index = [1 3 5 7 9]
That is fine if you declared even_index and odd_index appropriately. You
do, however, need declarations.
> somearray(even_index) = somearray(even_index) * 2.0
> somearray(odd_index) = somearray(odd_index) * 2.0
This is ok, once you defined even_index and odd_index appropriately. But
there is a lot simpler way. Just do
somearray(2::2) = somearray(2::2) * 2.0 !-- For the even ones
somearray(1::2) = somearray(1::2) * 2.0 !-- For the odd ones
> Is it possible? I get compiler error when I try it...
It helps to say what the compiler error is... and what code it goes
with, as you show multiple alternatives above.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain
Aha. Thanks.
> where I'm using the f2003 square bracket array constructor syntax. If
> your compiler doesn't accept that, substitute (/1,3,5,7/), but do note
> that you cannot use just (1,3,5,7).
>
>> even_index = [2 4 6 8 10]
>> odd_index = [1 3 5 7 9]
>
> That is fine if you declared even_index and odd_index appropriately. You
> do, however, need declarations.
Ok.
>> somearray(even_index) = somearray(even_index) * 2.0
>> somearray(odd_index) = somearray(odd_index) * 2.0
>
> This is ok, once you defined even_index and odd_index appropriately. But
> there is a lot simpler way. Just do
>
> somearray(2::2) = somearray(2::2) * 2.0 !-- For the even ones
> somearray(1::2) = somearray(1::2) * 2.0 !-- For the odd ones
That's much better, thanks.
>> Is it possible? I get compiler error when I try it...
>
> It helps to say what the compiler error is... and what code it goes
> with, as you show multiple alternatives above.
Well, I see you figured it out anyway. If you want to see the specific
error messages, which I personally find irrelevant, you could just make
a test-file and try what I suggested (which ofcourse wasn't very good
suggestions because I didn't knew any better)...
Even though I didn't try your suggestions yet, I would like to thank you
for your help because AFAICS it looks like you know what you're talking
about and I don't see any reason why your suggestions shouldn't work...
Thanks.
Regards,
Martin
> Richard Maine wrote:
> > It helps to say what the compiler error is... and what code it goes
> > with, as you show multiple alternatives above.
>
> Well, I see you figured it out anyway. If you want to see the specific
> error messages, which I personally find irrelevant, you could just make
> a test-file and try what I suggested
One could do a lot of things, but
1. We didn't have exactly whatever you tried. We just had isolated lines
out of context. The declaration stood pretty well alone, but for the
other lines, context was pretty much everything. Those lines would have
worked in the right context, namely with appropriate declarations. In
fact a *VERY* frequent reply to requests for help here is to see the
declarations that the poster failed to show, insomuch as the problems
are about as often in the declarations as in the code shown.
2. The messages might not mean much to you, but they *DO* generally mean
something to the people you are asking for help. Even if the messages
are completely off-base, an experienced progarmmer can sometimes
recognize what the compiler appears to be confused about and thsu reason
back to where the likely problem is.
3. When asking for help, it works a lot better if you do the work to
make things simpler for the person helping you. Saying that someone else
could just go generate a test case himself in order to help you is not
likely to be the most fruitful way to get help. Specifically, if I
hadn't been able to guess the problem without it, no I would not have
bothered.
All this is just for future reference, of course.
You think error messages generated by compilers are irrelevant? I guess we'll be hearing a
lot more from you in this newsgroup. :o)
Just kidding, of course. I'm assuming by "irrelevant" you meant the messages are difficult
to interpret (usually true), rather than "unrelated to the matter being considered."
(typically not true. :o)
cheers,
paulv