Work with variable

Yohn

Member
Hy!
I need to wrote Yohn with one variable and display it
as nhoY.

It need to display that.

thx.
 

Casper

ProgressTalk.com Moderator
Staff member
something like:

Code:
DEFINE VARIABLE cName AS CHARACTER  NO-UNDO.
DEFINE VARIABLE invertName AS CHARACTER  NO-UNDO.
DEFINE VARIABLE iTmp AS INTEGER    NO-UNDO.
 
ASSIGN cName = 'Yohn'.
DO iTmp = LENGTH(cName) TO 1 BY -1:
    ASSIGN invertName = invertName + SUBSTRING(cName,iTmp,1).
END.
MESSAGE invertName
    VIEW-AS ALERT-BOX INFO BUTTONS OK.

Casper.
 

Yohn

Member
no, but my college want's that I do that on that way.
I told him that this is impossible.
 

DevTeam

Member
Casper's idea, with 2 variables instead of 3 :

Code:
DEFINE VARIABLE cName AS CHARACTER  NO-UNDO.
DEFINE VARIABLE iTmp AS INTEGER    NO-UNDO.

ASSIGN cName = 'Yohn|'.

DO iTmp = LENGTH(cName) - 1 TO 1 BY -1:
  ASSIGN cName = cName + SUBSTRING(cName,iTmp,1).
END.

MESSAGE ENTRY(2,cName,'|') VIEW-AS ALERT-BOX INFO BUTTONS OK.
You may also tell your colleague that Variable declarations aren't incompatible with performance.

EDIT : I also assume it isn't homework for school, because I can't imagine a teacher taking Progress as a training developing language !
 

jdgibson

New Member
FUNCTION rev RETURNS CHAR(INPUT ll AS CHAR).
IF LENGTH(ll) = 1 THEN
RETURN ll.
ELSE
RETURN rev(SUBSTRING(ll,2)) + SUBSTRING(ll,1,1).
END.

DISP rev("Yohn").
 

MrMoo

Member
Ahh good ol recursion.
Would you need a case for an empty string as well or would this program just return when it encountered that situation?
 
Simpler (untested):

FUNCTION rev RETURNS CHAR(INPUT ll AS CHAR).

if ll = 'yohn' then return 'nhoy'.
else
return ?.

END.

DISP rev("Yohn").
 

DevTeam

Member
I like that one!
Nice way to manipulate return-values....

Casper.

Nice code indeed, but it seems to crash with big character strings... I get a "-nb exceeded. Automatically increasing from <old value> to <new value>. (5407)"... :confused:

And with 500 char strings, your code seems to be faster
 

Casper

ProgressTalk.com Moderator
Staff member
lol :lol: :D

Simpler (untested):
FUNCTION rev RETURNS CHAR(INPUT ll AS CHAR).
if ll = 'yohn' then return 'nhoy'.
else
return ?.
END.
DISP rev("Yohn").
 

MrMoo

Member
Nice code indeed, but it seems to crash with big character strings... I get a "-nb exceeded. Automatically increasing from <old value> to <new value>. (5407)"... :confused:

And with 500 char strings, your code seems to be faster

How big of a character string did you try?
I am assuming this is because each run of the function rev calls itself so if you have a 400 character string it calls itself 400 times, which I think would explain why you are exceeding the nested block limits.
 

DevTeam

Member
How big of a character string did you try?
I am assuming this is because each run of the function rev calls itself so if you have a 400 character string it calls itself 400 times, which I think would explain why you are exceeding the nested block limits.

I get the error as soon as the string has more than ~ 950 characters.
 
The requirement to work with one variable is stupid, unless it's an academic exercise. And even then it's stupid. Is someone having a joke at your expense?

So, you could present the recursive solution, then lean backward in your chair, arms crossed over your head, and chuckle knowingly to your colleague about "of course, recursion isn't a suitable solution except in trivial cases old boy; stack overflow and all that...".

This smug, annoying, talk-the-talk approach is a good way to promotion in my experience.
 

MrMoo

Member
The requirement to work with one variable is stupid, unless it's an academic exercise. And even then it's stupid. Is someone having a joke at your expense?

So, you could present the recursive solution, then lean backward in your chair, arms crossed over your head, and chuckle knowingly to your colleague about "of course, recursion isn't a suitable solution except in trivial cases old boy; stack overflow and all that...".

This smug, annoying, talk-the-talk approach is a good way to promotion in my experience.

I think this was an academic exercise, he even states that, to see if the students understand the concept of recursion and can implement it properly, but perhaps the teacher wanted to see if the students would use recursion on their own on something small like reversing an individuals name without being told directly to use it or if they'd come up with some convoluted solution which didn't involve recursion but only used one variable.
 
I think this was an academic exercise, he even states that

Where? Looking back I see I (mis?)read 'college' as a misspelling of 'colleague' (like DevTeam I think), so perhaps you are right.

perhaps the teacher wanted to see if the students would use recursion on their own on something small like reversing an individuals name without being told directly to use it or if they'd come up with some convoluted solution which didn't involve recursion but only used one variable.

I also thought this was a possibility, but I'm not sure telling them to 'only use one variable' is a good way to do it.

Whatever, OP will get brownie points for explaining the overflow issue, and why recursion isn't a good solution in this case (without severe constraints on the input set and implementation).

He/She will of course be kicked out of class if my 'solution' is used.
 

MrMoo

Member
Where? Looking back I see I (mis?)read 'college' as a misspelling of 'colleague' (like DevTeam I think), so perhaps you are right.

Yeah I thought it odd he would say college instead of professor or something similar but took it to mean that's how they do things at his school, could be he meant colleague but if it was my colleague I'd have to smack them upside the head for the one variable request :)


I also thought this was a possibility, but I'm not sure telling them to 'only use one variable' is a good way to do it.

Whatever, OP will get brownie points for explaining the overflow issue, and why recursion isn't a good solution in this case (without severe constraints on the input set and implementation).

He/She will of course be kicked out of class if my 'solution' is used.
Oh I am not saying 'use only one variable' is a good way to do it but I had a few profs like that in university and they'd say things like that thinking they were being super helpful in first year programming courses without telling you exactly how to do it, well you'd get some kids pulling their hair out trying to figure out how to do it while only declaring one variable.

I'd say go for the comments pointing out flaws in the use of recursion, I mean if the prof is a good one he'll agree to the limitations and maybe he'll get a bonus mark, if he's not one who likes being 1-uped well I doubt he'll look upon you kindly after that, but you can be happy in knowing you're right while he blows a gasket.
 
Top