My first practice program

My first practice program

Anonymous
Not applicable
554 Views
9 Replies
Message 1 of 10

My first practice program

Anonymous
Not applicable

Hi!

I got my self an Autolisp book since year 1998 to start learining it.

And they give quite good examples and practice tasks.

So i'm trying to do my first practice task.

The idea is to get the last word of a string by using while looping and counting functions.

So i was thinking t count words backwards, but it didnät work aout. Can anybody give me ideas so i can try myself 🙂
Here is  my beginning code...

(defun c:lastword (/ N1 txt lastword)
(setq N1 0
      txt "My first program")	
  (while
    (>(strlen txt) N1)
    ( setq N1 (1+ N1))
    )
  (setq strlength (strlen txt))
  
(setq lastword	(substr txt strlength N1 ))
(princ lastword)
  )

 

 

 

0 Likes
555 Views
9 Replies
Replies (9)
Message 2 of 10

pbejse
Mentor
Mentor
Here...
(setq txt "My first program")
                (setq p (vl-string-position 32 txt nil t))
            	(substr txt (+ 2 p))

 

or do you want Vanilla code?

 

 

0 Likes
Message 3 of 10

Anonymous
Not applicable

I'm not awrae of these vla objects, so.....

0 Likes
Message 4 of 10

pbejse
Mentor
Mentor

HYG

 

(setq txt "My first program"
      slen (Strlen txt))     
  (while  (And (/= " " (substr txt slen 1))
               (> slen 1)
        	(Setq slen (1- slen))))
      
      
      (setq lastword	(substr txt (1+ slen))))

 

0 Likes
Message 5 of 10

Anonymous
Not applicable
Thanks!
It seems to be the one.
Can you put some explanation behind so i could study it better.
M
0 Likes
Message 6 of 10

Anonymous
Not applicable

Thanks!

It seems to be the one.

Can you put some explanation behind so i could study it better.

M

0 Likes
Message 7 of 10

Anonymous
Not applicable

Do i understand it rigth

(while  (And (/= " " (substr txt slen 1)); this condition will start counting backwards and stopis when first " " is met?
               (> slen 1)
        	(Setq slen (1- slen))))

 

 

0 Likes
Message 8 of 10

pbejse
Mentor
Mentor

@Anonymous wrote:

Do i understand it rigth

(while  (And (/= " " (substr txt slen 1)); this condition will start counting backwards and stopis when first " " is met?
               (> slen 1)
        	(Setq slen (1- slen))))

 

 


Correct. You got it Karla32, 

0 Likes
Message 9 of 10

pbejse
Mentor
Mentor

@Anonymous wrote:

Do i understand it rigth

(while; this condition will start counting backwards and stopis when first " " is met?

  


Correct. You got it Karla32, 

 

Ooosps. double posts ,hahaha

0 Likes
Message 10 of 10

scot-65
Advisor
Advisor
>>Can you put some explanation behind so i could study it better.

A WHILE loop will continue until the test becomes False (nil).
Your exercise uses AND - meaning two (or more) tests must
be True so the loop can iterate through another cycle.

Be careful with WHILE's as one can get trapped inside and the only
way to escape is to do a hard shutdown (even in w7, as I found out).

Where the test returned T = True and F = False (schematically):
While [AND T T] will keep the loop active.
While [AND T F] will escape the loop.
While [OR F T] will keep the loop active. One must be true, not both.
While [OR F F] will escape the loop.
While [NOT F] will keep the loop active. What is opposite of False?

Another method in another part of your program is to employ
the functions ASCII and CHR to compare a single character.
(ASCII " ") returns 32
(CHR 32) returns " " [a blank space].

Hope this helps.

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes