- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
Hi,
Thanks,
I am using Visual Fortran Compiler for Windows. My question is how to increase an array size, while keeping the previous values.
For example, if I have a(100) with 100 values stored, later I will need to increase a's size to 200, so a(200). However I would like to keep the previous 100 values and start populating with new values from 101...200.
Any suggestions?
Thanks,
Roberto
Link kopiert
1 Antworten
- Als neu kennzeichnen
- Lesezeichen
- Abonnieren
- Stummschalten
- RSS-Feed abonnieren
- Kennzeichnen
- Anstößigen Inhalt melden
You cannot "extend" an existing array, but you can create a new one with the old one's data. This is best done using ALLOCATABLE arrays and the MOVE_ALLOC intrinsic, which lets you do it with one copy instead of two. Something like this:
REAL, ALLOCATABLE :: A(:)
REAL, ALLOCATABLE :: AX(:) ! Extended copy of A
...
ALLOCATE (A(100))
... A now has 100 elements
ALLOCATE (AX(200)) ! Create a new array of the larger size
AX(1:100) = A ! Copy in existing elements
CALL MOVE_ALLOC (AX,A)
... A is now 1:200 and AX is deallocated
Of course, you may be able to avoid this by allocating the array to the needed larger size initially, but for cases where you need to "extend", the above is the way to do it in Fortran.
REAL, ALLOCATABLE :: A(:)
REAL, ALLOCATABLE :: AX(:) ! Extended copy of A
...
ALLOCATE (A(100))
... A now has 100 elements
ALLOCATE (AX(200)) ! Create a new array of the larger size
AX(1:100) = A ! Copy in existing elements
CALL MOVE_ALLOC (AX,A)
... A is now 1:200 and AX is deallocated
Of course, you may be able to avoid this by allocating the array to the needed larger size initially, but for cases where you need to "extend", the above is the way to do it in Fortran.
Antworten
Themen-Optionen
- RSS-Feed abonnieren
- Thema als neu kennzeichnen
- Thema als gelesen kennzeichnen
- Diesen Thema für aktuellen Benutzer floaten
- Lesezeichen
- Abonnieren
- Drucker-Anzeigeseite