Lahey Support
08-15-2003, 01:19 AM
Swap variables...
Is this to avoid declaring the temp variable to
save space or to avoid polluting the code with temps?
This is difficult to solve for all possible variable
types and kinds. With integers, how about the triple XOR:
write(unit=*,fmt=*) "integer a?"
read(unit=*,fmt=*) a
write(unit=*,fmt=*) "integer b?"
read(unit=*,fmt=*) b
a = ieor(a,b)
b = ieor(a,b)
a = ieor(a,b)
write(unit=*,fmt=*) "swapped them:"
write(unit=*,fmt=*) "a=",a," b=",b
If they aren't integers, then it seems that you might
use a new subroutine for each new variable type and kind.
With a swap(a,b) subroutine, an automatic variable can be
introduced without polluting the rest of the code.
subroutine swapStr(a,b)
character(len=*),intent(in out) :: a,b
character(len=len(a)) :: c
if (len(a) /= len(b)) then
write(unit=*,fmt=*) "string lengths unequal!"
stop
end if
c = a
a = b
b = c
return
end subroutine swapStr
--
Michael J. White, [address removed]
Applied Research Associates, Inc., http://www.ara.com
5941 S. Middlefield Rd., Littleton, CO 80123 USA
(303)795-8106, fax:(303)795-8159
List Server wrote:
>
> ----------------------------------------------------------------------
> To post a reply to the list, you must send it to [address removed]
> ----------------------------------------------------------------------
> Date: Sat, 30 Jan 1999 15:16:31 +0300 (SAUST)
> From: "Al Salamah, Muhammad" [address removed]
> Subject: [LF] swaping
>
> Hi there
> I and most of the people I work with face the problem of swapping the
> values of two variables without ever needing a third variable. Can any body
> offer a solution. Thanks.
>
> ************************************************
> ************************************************
> ** Muhammad Al Salamah **
> ** KFUPM **
> ** Box 382 **
> ** Dhahran 31261 **
> ** Saudi Arabia **
> ** Phone No.: Residence +(966)38606045 **
> ** Office +(966)38602541 **
> ************************************************
> ************************************************
Is this to avoid declaring the temp variable to
save space or to avoid polluting the code with temps?
This is difficult to solve for all possible variable
types and kinds. With integers, how about the triple XOR:
write(unit=*,fmt=*) "integer a?"
read(unit=*,fmt=*) a
write(unit=*,fmt=*) "integer b?"
read(unit=*,fmt=*) b
a = ieor(a,b)
b = ieor(a,b)
a = ieor(a,b)
write(unit=*,fmt=*) "swapped them:"
write(unit=*,fmt=*) "a=",a," b=",b
If they aren't integers, then it seems that you might
use a new subroutine for each new variable type and kind.
With a swap(a,b) subroutine, an automatic variable can be
introduced without polluting the rest of the code.
subroutine swapStr(a,b)
character(len=*),intent(in out) :: a,b
character(len=len(a)) :: c
if (len(a) /= len(b)) then
write(unit=*,fmt=*) "string lengths unequal!"
stop
end if
c = a
a = b
b = c
return
end subroutine swapStr
--
Michael J. White, [address removed]
Applied Research Associates, Inc., http://www.ara.com
5941 S. Middlefield Rd., Littleton, CO 80123 USA
(303)795-8106, fax:(303)795-8159
List Server wrote:
>
> ----------------------------------------------------------------------
> To post a reply to the list, you must send it to [address removed]
> ----------------------------------------------------------------------
> Date: Sat, 30 Jan 1999 15:16:31 +0300 (SAUST)
> From: "Al Salamah, Muhammad" [address removed]
> Subject: [LF] swaping
>
> Hi there
> I and most of the people I work with face the problem of swapping the
> values of two variables without ever needing a third variable. Can any body
> offer a solution. Thanks.
>
> ************************************************
> ************************************************
> ** Muhammad Al Salamah **
> ** KFUPM **
> ** Box 382 **
> ** Dhahran 31261 **
> ** Saudi Arabia **
> ** Phone No.: Residence +(966)38606045 **
> ** Office +(966)38602541 **
> ************************************************
> ************************************************