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

logical issue in code


IBM Mainframe Forums -> CLIST & REXX
Post new topic   Reply to topic
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
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: 1788
Location: Bloomington, IL

PostPosted: Wed Apr 25, 2012 11:56 pm
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

Superior Member


Joined: 14 Mar 2007
Posts: 10872
Location: italy

PostPosted: Thu Apr 26, 2012 12:00 am
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
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: 2455
Location: Hampshire, UK

PostPosted: Thu Apr 26, 2012 12:08 pm
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

Superior Member


Joined: 14 Mar 2007
Posts: 10872
Location: italy

PostPosted: Thu Apr 26, 2012 12:17 pm
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

Superior Member


Joined: 14 Mar 2007
Posts: 10872
Location: italy

PostPosted: Thu Apr 26, 2012 12:26 pm
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

Senior Member


Joined: 07 Feb 2009
Posts: 1306
Location: Vilnius, Lithuania

PostPosted: Thu Apr 26, 2012 1:56 pm
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

Superior Member


Joined: 14 Mar 2007
Posts: 10872
Location: italy

PostPosted: Thu Apr 26, 2012 2:05 pm
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: 2455
Location: Hampshire, UK

PostPosted: Fri Apr 27, 2012 2:34 am
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> CLIST & REXX

 


Similar Topics
Topic Forum Replies
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts run rexx code with jcl CLIST & REXX 15
No new posts Compile rexx code with jcl CLIST & REXX 6
No new posts Rotate partition-logical & physic... DB2 0
No new posts REXX code to expand copybook in a cob... CLIST & REXX 2
Search our Forums:

Back to Top