Author Topic: (Challange) Edit part of text string.  (Read 3785 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
(Challange) Edit part of text string.
« on: November 15, 2004, 01:41:24 PM »
This challenge will be broken down into two parts. One part being this part and the next part will be being the ...next part. (*blink*)

I want to be able to edit just part of a text string. (Okay, i can hear what your saying; -"Duh!? ...he be stupid!" but bear with me.)

If i have a text string like so: "Jack and Jill ran up the hill" And most of this text will NEVER change except for one part; "Jack" (I guess Jill gets arround *LoL*. ...*sigh* ok, I'm sorry for that. that was just bad!?) So wouldnt it be nice to lock up that text so when you go to edit that string only a little piece of text is editable?!

How about we do this: We have a set start point and a end point marker. Lets use brackets. '<' & '>' So our string would/should look like this: "<Jack> and Jill ran up the hill." And when we go to edit the string, all we give to the user is "Jack".

How about that. ...Okay that isnt very hard at all but the next part might be.

So your challenge is to build me a procedure that will prompt me only text between the brackets.  Here is your string: "<Jack> and Jill ran up the hill."

TIP: Those of you new to lisp, should get in on this one as well, this will not be a hard procedure at all.  In fact, I will help you out a bit. Look into the AutoLisp function: "subst" and see if you can think of a way to test the overall string for those brackets.  The main workhorse of this challange will be a procedure that will acccept as an argument a string, string position, and length. ...trust me. If you get stuck, just sit back and see if you were on the right track. but try to get something down on paper before looking at other peoples code.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
(Challange) Edit part of text string.
« Reply #1 on: November 15, 2004, 01:51:30 PM »
so the it might look something like ........
(defun func (str-edit str)
  ( ........
   ......... )
  )
TheSwamp.org  (serving the CAD community since 2003)

Jeff_M

  • King Gator
  • Posts: 4100
  • C3D user & customizer
(Challange) Edit part of text string.
« Reply #2 on: November 15, 2004, 01:56:37 PM »
Well, here's how I'd do it.....allowing only one  string to be selected at a time:

<snip> of partial code.....

next time I'll read the whole post before submitting.......

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
(Challange) Edit part of text string.
« Reply #3 on: November 15, 2004, 01:59:37 PM »
Nope,

(defun procedure (string)
  (....)
)

>: "Jack"

I dont want to select anything, this challenge is a workhorse. Just a procedure to "parse a string" so to speak.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
(Challange) Edit part of text string.
« Reply #4 on: November 15, 2004, 02:07:20 PM »
No thats cool. Save that, cause we might need that later. This is actualy turning out to be a cool idea.  (I'm doing some brain storming now.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Jeff_M

  • King Gator
  • Posts: 4100
  • C3D user & customizer
(Challange) Edit part of text string.
« Reply #5 on: November 15, 2004, 02:18:13 PM »
OK, better?
Code: [Select]

(defun ed_str (str / p1 p2)
  (if (and (setq p1 (vl-string-position (ascii "<") str))
  (setq p2 (vl-string-position (ascii ">") str))
  )
    (substr str (+ 2 p1) (1- p2))
    )
  )

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(Challange) Edit part of text string.
« Reply #6 on: November 15, 2004, 02:47:16 PM »
Do it with lists 8)
Code: [Select]
(defun ed_str (str / slst target)
  (and
    (setq slst (vl-string->list str))
    (setq target (cdr(member 60 slst)))
    (setq target (cdr(member 62 (reverse target))))
    (setq target (vl-list->string (reverse target)))
  )
  Target
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
(Challange) Edit part of text string.
« Reply #7 on: November 15, 2004, 03:53:21 PM »
Cool.   ...remeber to comment your code for the newbies too.

(Challenge: II) Parse a given string.

Ok, this challenge is kinda turning out to be a good idea so, I'm gonna extend this into a few more parts.

Your challenge is to make a procedure that will return several things.
1.) The string parsed of brackets.
2.) The position where the first braket was. (zero bassed)
3.) The length of chars inside the brackets.

Given the string: "<Jack> and Jill went up the hill."

Your procedure should return: "Jack and Jill went up the hill." 0 4
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

whdjr

  • Guest
(Challange) Edit part of text string.
« Reply #8 on: November 15, 2004, 04:02:37 PM »
Can the string within the brackets contain more than one word:

"<Jack Brown> and Jill went up the hill."

"<Lumber Jack> and Jill went up the hill."

"<Statue of Liberty> and Jill went up the hill."

or should the string within the brackets be constrained to one word only:

Just a thought.

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
(Challange) Edit part of text string.
« Reply #9 on: November 15, 2004, 05:07:45 PM »
Multiple words are A-ok.  (spaces are char's too!?)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10669
(Challange) Edit part of text string.
« Reply #10 on: November 15, 2004, 05:09:20 PM »
Oh, and "Jack and <Jill> went up the hill." is cool too.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
(Challange) Edit part of text string.
« Reply #11 on: November 15, 2004, 05:14:40 PM »
Code: [Select]
;;  returns list (string  target [start pos] length)
(defun ed_str (str / slst pre tar suf)
  (setq slst (vl-string->list str)
        pre  '() ; empty list
        tar  '()
        suf  '()
  )
  (while (and (> (length slst) 0) ; get the prefix
              (/= (car slst) 60)
         )
    (setq pre  (cons (car slst) pre)
          slst (cdr slst)
    )
  )
  (setq slst (cdr slst)) ; remove <
  (while (and (> (length slst) 0) ; get the target
              (/= (car slst) 62)
         )
    (setq tar  (cons (car slst) tar)
          slst (cdr slst)
    )
  )
  (setq slst (cdr slst)) ; remove >
  (while (> (length slst) 0) ; get the suffix
    (setq suf  (cons (car slst) suf)
          slst (cdr slst)
    )
  )
  (setq start (length pre))
  (setq len (length tar))
  (setq str (vl-list->string (append (reverse pre) (reverse tar) (reverse suf))))
  (setq tar (vl-list->string (reverse tar)))
  (list str tar start len)
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
(Challange) Edit part of text string.
« Reply #12 on: November 19, 2004, 12:39:58 PM »
Okay, I'm still on part one, but here's my take on it, minus the brackets thing. I'm not sure I understand the reason for them. Here:
Code: [Select]
(stringsearch
     "john"
     "john get's a dear john from jenny"
     "Se7en"
)
(defun stringsearch (lookup string sub)
     (while
 (vl-string-search lookup string)
     (setq string
(vl-string-subst
    sub
    lookup
    string
)
     )
     )
)


That little ditty will continue to look for the word john, replacing it with Se7en until it reaches the end of the string or whatever you supply as your arguments.

Am I close to what you're asking for Se7en?