View previous topic :: View next topic
|
Author |
Message |
yogeshwar_ade
Active User
Joined: 31 Aug 2006 Posts: 103 Location: INDIA
|
|
|
|
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 |
|
|
Akatsukami
Global Moderator
Joined: 03 Oct 2009 Posts: 1788 Location: Bloomington, IL
|
|
|
|
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 |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10872 Location: italy
|
|
|
|
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 |
|
|
yogeshwar_ade
Active User
Joined: 31 Aug 2006 Posts: 103 Location: INDIA
|
|
|
|
Thanks enrico and Akatsukami
It's working correctly now. |
|
Back to top |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2455 Location: Hampshire, UK
|
|
|
|
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 |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10872 Location: italy
|
|
|
|
I beg to disagree
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 |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10872 Location: italy
|
|
|
|
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 |
|
|
prino
Senior Member
Joined: 07 Feb 2009 Posts: 1306 Location: Vilnius, Lithuania
|
|
|
|
Enrico,
Now try with PARSE, that might be even faster. |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10872 Location: italy
|
|
|
|
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 |
|
|
Nic Clouston
Global Moderator
Joined: 10 May 2007 Posts: 2455 Location: Hampshire, UK
|
|
|
|
I'll just go and crawl back into my hole! (But it was a nice curry!) |
|
Back to top |
|
|
|