IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

How to swap values in 2 fields


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
ashjain79
Warnings : 1

New User


Joined: 09 Nov 2010
Posts: 1
Location: mumbai

PostPosted: Sun Nov 21, 2010 7:33 am
Reply with quote

Hi All,

Can Anyone tell me about given below query solution.


If we have 01 Data-1 pic 9(02) value 25.
01 Date-2 pic 9(02) value 15.


we want to swap these two value. Data-2 = 25, Data-1 = 15.

How i can swap the value using cobol.

Regards,
Ashish
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sun Nov 21, 2010 7:59 am
Reply with quote

Hello,

When you have a question you should start a new topic for your question rather than posting a reply to some existing question.

To do what you want, define a 3rd variable and do a 3-way move.
Back to top
View user's profile Send private message
sreekanth1984

New User


Joined: 23 Mar 2010
Posts: 22
Location: Bangalore

PostPosted: Mon Nov 22, 2010 5:37 pm
Reply with quote

Hi ashjain79,

If you want to acheive this with out using third varible, you can do it by subtracting 10 from Data-1 and adding 10 to Data-2.


Thanks
Srikanth
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Mon Nov 22, 2010 5:42 pm
Reply with quote

Quote:
If you want to acheive this with out using third varible, you can do it by subtracting 10 from Data-1 and adding 10 to Data-2.


looks like today is the contest day for the silliest answer ???
Back to top
View user's profile Send private message
Rahul Agrawal

New User


Joined: 19 Jul 2010
Posts: 9
Location: Noida

PostPosted: Mon Nov 22, 2010 5:50 pm
Reply with quote

Hi Ashjain79,

You can use the following formula to swap the values
CASE A:
Without using 3rd variable.

Solution1:
Data-1 = Data-1 + Data-2
Data-2 = Data-1 - Data-2
Data-1 = Data-1 - Data-2

Solution2:
Data-1 = Data-1 * Data-2
Data-2 = Data-1 / Data-2
Data-1 = Data-1 / Data-2

CASE B:
Using third variable (Temp-var).

Temp-var = Data-1
Data-1 = Data-2
Data-2 = Temp-var
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Mon Nov 22, 2010 6:11 pm
Reply with quote

Rahul Agrawal,

the TS defined his fields as unsigned.
Code:

If we have 01 Data-1 pic 9(02) value 25.
01 Date-2 pic 9(02) value 15.


solution 1 will not work.

solution 2 will work in the tight parameters of this example.

but what if one of the fields was defined as 9(2)v9(2)?

and, if the fields were not numeric, you would have a problem.

you answers should have been qualified, as to when they will work and when they will not.

and since everybody seems to be hung-up with effeciency,
moves with an iterm field will always be faster than arithmetics.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Mon Nov 22, 2010 6:22 pm
Reply with quote

swapping values without any auxiliary variable has too many quirks
to make it valuable as a general solution

for the pic 9(2) might with the values posted ( 25 and 15 ) because the sum fits into a 9(2) pic ..
what if the values were 63 and 79 ???

for equal length operands the safest would be 3 xor' s
Back to top
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1042
Location: Richmond, Virginia

PostPosted: Mon Nov 22, 2010 8:05 pm
Reply with quote

I propose a new section: Questions of academic interest but not for practical use.

I'm all for academic questions, but real production code must 2nd-ly (1st-ly it must be correct) be eminently readable by an entry level coder who will inherit it and be tasked with making excruciatingly complex mods.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Mon Nov 22, 2010 8:07 pm
Reply with quote

Quote:
Questions of academic interest but not for practical use.


AKA stupid questions icon_biggrin.gif
Back to top
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1042
Location: Richmond, Virginia

PostPosted: Mon Nov 22, 2010 8:12 pm
Reply with quote

I respectfully disagree. (Actually Enrico, I know you agree with me. But you get too involved in the stupid Q's - you gotta let them roll off a bit.)

There is much fun and learning to be had from academic questions and approaches - even challenges.

But squeezing nanoseconds out of a program at the expense of dense code is hard to justify in the real world.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Mon Nov 22, 2010 8:16 pm
Reply with quote

my issue is that I respect too much IT to let stupid questions slip by
and note that I do not pick on all of them icon_biggrin.gif

as soon as I have a bit of time i' ll let You know if it is faster to use an auxiliary variable or three xor's
Back to top
View user's profile Send private message
Phrzby Phil

Senior Member


Joined: 31 Oct 2006
Posts: 1042
Location: Richmond, Virginia

PostPosted: Mon Nov 22, 2010 8:19 pm
Reply with quote

And of course please let us know how many will have be executed the better way to equal the time you spend finding out, and include my time for the previous posts, and this post, and this part of this sentence, ........................disconnect
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Mon Nov 22, 2010 8:35 pm
Reply with quote

Hello,

Well, i'm sure no one wants a replay of a compiler (and quite a bit more) i once co-authored , but to provide for this we provided 3 instructions that our programmers kinda liked:
Compare And Swap High (cswaphi)
Compare And Swap Low (cswaplo)
Swap (swap)

These were quite handy when manipulating "stuff". We dynamically allocated a bit of memory to use for the 3rd field and the programmer did not need to define it. . .

Ah, the old days. . . icon_smile.gif

I would not expect to find this in "standard" COBOL any time soon. . . icon_wink.gif

d
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Mon Nov 22, 2010 11:28 pm
Reply with quote

research mood on


I just run a few test on swapping values
using the hercules emulator running on
an iMac quad core 2.93ghz
loop executed 1000000000 times

exchange two 32 bits registers
move ( LR ) cpu 0:25.03
xor ( XR ) cpu 0:24.61

exchange two 4 bytes variables
move ( MVC ) cpu 1:11.12
xor ( XC ) cpu 1:13.44

I wonder what would be the difference on a real maiframe ???

exchange two registers
Code:

TESTXOR  CSECT
         USING TESTXOR,12
         STM   14,12,12(13)
         LR    12,15
         L     11,=F'1000000000'
LOOP     DS    0H
         XR    2,3
         XR    3,2
         XR    2,3
         BCT   11,LOOP
         L     14,12(13)
         SR    15,15
         BR    14
         LTORG
         DS    0D
A        DC    CL4'0123456789ABCDEF'
B        DC    CL4'FEDCBA9876543210'
C        DC    CL4'0000000000000000'
         END   TESTXOR

TESTMVC  CSECT
         USING TESTMVC,12
         STM   14,12,12(13)
         LR    12,15
         L     11,=F'1000000000'
LOOP     DS    0H
         LR    15,2
         LR    2,3
         LR    3,15
         BCT   11,LOOP
         L     14,12(13)
         SR    15,15
         BR    14
         LTORG
         DS    0D
A        DC    CL4'0123456789ABCDEF'
B        DC    CL4'FEDCBA9876543210'
C        DC    CL4'0000000000000000'
         END   TESTMVC


exchange two variables
Code:

TESTXOR  CSECT
         USING TESTXOR,12
         STM   14,12,12(13)
         LR    12,15
         L     11,=F'1000000000'
LOOP     DS    0H
         XC    A,B
         XC    B,A
         XC    A,B
         BCT   11,LOOP
         L     14,12(13)
         SR    15,15
         BR    14
         LTORG
         DS    0D
A        DC    CL4'0123456789ABCDEF'
B        DC    CL4'FEDCBA9876543210'
C        DC    CL4'0000000000000000'
         END   TESTXOR

TESTMVC  CSECT
         USING TESTMVC,12
         STM   14,12,12(13)
         LR    12,15
         L     11,=F'1000000000'
LOOP     DS    0H
         MVC   C,A
         MVC   A,B
         MVC   B,C
         BCT   11,LOOP
         L     14,12(13)
         SR    15,15
         BR    14
         LTORG
         DS    0D
A        DC    CL4'0123456789ABCDEF'
B        DC    CL4'FEDCBA9876543210'
C        DC    CL4'0000000000000000'
         END   TESTMVC


the difference is not even worth meditating about it
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts INCLUDE OMIT COND for Multiple values... DFSORT/ICETOOL 5
No new posts Replace Multiple Field values to Othe... DFSORT/ICETOOL 12
No new posts Null values are considered in Total c... DFSORT/ICETOOL 6
No new posts Converting ASCII values to COMP-3 (ZD... JCL & VSAM 2
No new posts Generate output lines (SYSIN card for... DFSORT/ICETOOL 4
Search our Forums:

Back to Top