Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

logical issue in code

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> CLIST & REXX
View previous topic :: :: View next topic  
Author Message
yogeshwar_ade

Active User


Joined: 31 Aug 2006
Posts: 103
Location: INDIA

PostPosted: Wed Apr 25, 2012 11:28 pm    Post subject: logical issue in code
Reply with quote

Hi


I have written below code
Code:
I = 1
DO FREC.0
   WREC = FREC.I
   SELECT
    WHEN SUBSTR(WREC,I,1) = '*' THEN
       DO 
          SAY ' I AM IN * CASE'
       END
      WHEN SUBSTR(WREC,I,2) = '--' THEN
        DO 
           SAY ' I AM IN - CASE'
        END
     OTHERWISE
         SAY FREC.I
      END
      I = I +1
 END

Here I am reading below file into FREC which have below three records.
*first record
second record
--third record

Here I am expecting output
I AM IN * CASE
second record
I AM IN -- CASE

But I am getting below results
I AM IN * CASE
second record
--third record

Could you please let me know where I am making mistake.

Also can you suggest coding on below case

if record has '--' sign in the middle of record – copy the part of record to the left of '--' and ignore the remaining part
Back to top
View user's profile Send private message

Akatsukami

Global Moderator


Joined: 03 Oct 2009
Posts: 1738
Location: Bloomington, IL

PostPosted: Wed Apr 25, 2012 11:56 pm    Post subject: Re: logical issue in code
Reply with quote

yogeshwar_ade wrote:
Hi


I have written below code
Code:
I = 1
DO FREC.0
   WREC = FREC.I
   SELECT
    WHEN SUBSTR(WREC,I,1) = '*' THEN
       DO 
          SAY ' I AM IN * CASE'
       END
      WHEN SUBSTR(WREC,I,2) = '--' THEN
        DO 
           SAY ' I AM IN - CASE'
        END
     OTHERWISE
         SAY FREC.I
      END
      I = I +1
 END


Here I am reading below file into FREC which have below three records.
Code:
*first record
second record
--third record


Here I am expecting output
I AM IN * CASE
second record
I AM IN -- CASE

But I am getting below results
I AM IN * CASE
second record
--third record

Could you please let me know where I am making mistake.


Always enclose code -- and any other information where alignment is significant -- in Code tags, as I have done to your quoted code.

Now, had you tested your code with a TRACE option, it had been immediately obvious to you that the problem is the clause
Code:
WHEN SUBSTR(WREC,I,2) = '--'

since when you process the third record I = 3, this is interpreted as
Code:
WHEN SUBSTR(WREC,3,2) = '--'

which is false. You should have coded
Code:
WHEN SUBSTR(WREC,1,2) = '--'


(The clause
Code:
WHEN SUBSTR(WREC,I,1) = '*'

is also wrong, but works by accident, since it is the first record that has the leading asterisk; for any other record, it wouldn't work.

Quote:
Also can you suggest coding on below case

if record has '--' sign in the middle of record – copy the part of record to the left of '--' and ignore the remaining part

Trivial:
Code:
p = pos('--',record)

if (p>1) then
  car = substr(record,1,p-1)
else
  car = ''
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Thu Apr 26, 2012 12:00 am    Post subject: Reply to: logical issue in code
Reply with quote

You are not checking the first position of the data
You are checking a position dependent on the record number

no reason to increment Yourself the Do Index
for the outer loop wiser to use a better named index


Code:
do  irec = 1 to frec.0
    wrec = frec.irec
    select
        when substr(wrec,1,1) = "*" then do
            say "found '*'  in record number" irec
        end
        when substr(wrec,1,2) = "--" then do
            say "found '--' in record number" irec
        end
        otherwise do
            say irec wrec
        end
    end
end
Back to top
View user's profile Send private message
yogeshwar_ade

Active User


Joined: 31 Aug 2006
Posts: 103
Location: INDIA

PostPosted: Thu Apr 26, 2012 8:55 am    Post subject:
Reply with quote

Thanks enrico and Akatsukami

It's working correctly now.
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 1715
Location: UK

PostPosted: Thu Apr 26, 2012 12:08 pm    Post subject:
Reply with quote

also - extract the 2 bytes into a variable before the select and then check on that variable. Saves doing the extract at each WHEN.
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Thu Apr 26, 2012 12:17 pm    Post subject: Reply to: logical issue in code
Reply with quote

I beg to disagree icon_wink.gif

the first comparison is for ONE byte,
the second one is for TWO bytes

it might be interesting to see the difference in performance
using the LEFT function instead of the SUBSTR
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Thu Apr 26, 2012 12:26 pm    Post subject: Reply to: logical issue in code
Reply with quote

faster to check than speculate

with LEFT
Code:

#!/usr/bin/rexx
parse arg coun
s = "some"
do  coun
    s1 = left(s,2)
end

$time ./t.rx 100000000

real   0m16.010s
user   0m15.876s
sys   0m0.038s


with SUBSTR
Code:

#!/usr/bin/rexx
parse arg coun
s = "some"
do  coun
    s1 = substr(s,1,2)
end

$time ./t.rx 100000000

real   0m17.407s
user   0m17.354s
sys   0m0.029s


the difference probably is due to accessing the variable pool three times instead of two
Back to top
View user's profile Send private message
prino

Active Member


Joined: 07 Feb 2009
Posts: 984
Location: Oostende, Belgium

PostPosted: Thu Apr 26, 2012 1:56 pm    Post subject:
Reply with quote

Enrico,

Now try with PARSE, that might be even faster.
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10203
Location: italy

PostPosted: Thu Apr 26, 2012 2:05 pm    Post subject: Reply to: logical issue in code
Reply with quote

Hi Robert,
unfortunately object Rexx is not of the same opinion
( regina Rexx even worse )

Code:
#!/usr/bin/rexx
parse arg coun
s = "some"
do  coun
    parse var s 1 s1 3 .
end


Code:

( Object Rexx )
time ./t.rx 100000000
real   0m17.843s
user   0m17.773s
sys   0m0.030s


( Regina static )
time /opt/regina/bin/rexx t.rx 100000000
real   0m20.362s
user   0m20.285s
sys   0m0.008s

( Regina dynam )
time /opt/regina/bin/regina t.rx 100000000
real   0m21.159s
user   0m21.131s
sys   0m0.009s


even worse for regina ( from a pure interpreter point of view)
considering that for oorexx some time is spent on shared library handling
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 1715
Location: UK

PostPosted: Fri Apr 27, 2012 2:34 am    Post subject:
Reply with quote

I'll just go and crawl back into my hole! (But it was a nice curry!)
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> CLIST & REXX All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts PL/I code tuning/Performance improvement Virendra Shambharkar PL/I & Assembler 4 Mon Dec 05, 2016 11:57 am
No new posts What is the code in CLIST to enable t... jackzhang75 CLIST & REXX 1 Fri Dec 02, 2016 3:02 am
No new posts IDEAL - Code Retrieval yugendran CA Products 0 Fri Nov 25, 2016 3:27 pm
No new posts DFHRESPONSE returns issue divated CICS 3 Wed Nov 02, 2016 6:32 pm
No new posts Can sending 5 MB data between cobol p... Kevin Vaz CICS 12 Tue Oct 18, 2016 4:50 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us