Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref

Author Message
ashjain79
Warnings : 1

New User

Joined: 09 Nov 2010
Posts: 2
Location: mumbai

 Posted: Sun Nov 21, 2010 7:33 am    Post subject: Cobol/Ques 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

dick scherrer

Site Director

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

 Posted: Sun Nov 21, 2010 7:59 am    Post subject: 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.
sreekanth1984

New User

Joined: 23 Mar 2010
Posts: 22
Location: Bangalore

 Posted: Mon Nov 22, 2010 5:37 pm    Post subject: 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
enrico-sorichetti

Senior Member

Joined: 14 Mar 2007
Posts: 10603
Location: italy

Posted: Mon Nov 22, 2010 5:42 pm    Post subject: Reply to: How to swap values in 2 fields

 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 ???
Rahul Agrawal

New User

Joined: 19 Jul 2010
Posts: 9
Location: Noida

 Posted: Mon Nov 22, 2010 5:50 pm    Post subject: 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
dbzTHEdinosauer

Global Moderator

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

Posted: Mon Nov 22, 2010 6:11 pm    Post subject:

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.
enrico-sorichetti

Senior Member

Joined: 14 Mar 2007
Posts: 10603
Location: italy

 Posted: Mon Nov 22, 2010 6:22 pm    Post subject: Reply to: How to swap values in 2 fields 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
Phrzby Phil

Active Member

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

 Posted: Mon Nov 22, 2010 8:05 pm    Post subject: 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.
enrico-sorichetti

Senior Member

Joined: 14 Mar 2007
Posts: 10603
Location: italy

Posted: Mon Nov 22, 2010 8:07 pm    Post subject: Reply to: How to swap values in 2 fields

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

AKA stupid questions
Phrzby Phil

Active Member

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

 Posted: Mon Nov 22, 2010 8:12 pm    Post subject: 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.
enrico-sorichetti

Senior Member

Joined: 14 Mar 2007
Posts: 10603
Location: italy

 Posted: Mon Nov 22, 2010 8:16 pm    Post subject: Reply to: How to swap values in 2 fields 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 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
Phrzby Phil

Active Member

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

 Posted: Mon Nov 22, 2010 8:19 pm    Post subject: 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
dick scherrer

Site Director

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

 Posted: Mon Nov 22, 2010 8:35 pm    Post subject: Reply to: How to swap values in 2 fields 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. . . I would not expect to find this in "standard" COBOL any time soon. . . d
enrico-sorichetti

Senior Member

Joined: 14 Mar 2007
Posts: 10603
Location: italy

Posted: Mon Nov 22, 2010 11:28 pm    Post subject: Reply to: How to swap values in 2 fields

research mood on

I just run a few test on swapping values
using the hercules emulator running on
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
 All times are GMT + 6 Hours
 Page 1 of 1

Search our Forum:

 Topic Author Forum Replies Posted Similar Topics Need to get the SUM of two fields usi... Poha Eater DFSORT/ICETOOL 7 Tue Jun 18, 2019 1:27 pm Compare fields with decimal chandan.inst SYNCSORT 6 Thu Apr 04, 2019 5:29 pm Query on summing up the fields abdulrafi DFSORT/ICETOOL 10 Mon Jan 21, 2019 7:18 pm How can we create a flat file in JAVA... jasorn Java & MQSeries 2 Wed Aug 08, 2018 3:08 pm remove the gap b/w 2 fields shymala COBOL Programming 5 Thu Aug 02, 2018 12:05 pm

 © 2003-2017 IBM MAINFRAME Software Support Division
 Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us