Author Topic: Using 'wcmatch' ...  (Read 4254 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Using 'wcmatch' ...
« Reply #15 on: June 07, 2010, 07:42:42 PM »
You know, it really cracks me up when I see answers like this.
Not your answer, but my fault.  You see, I have other code that is formatted identically, and it works without a hitch.  I get the same thing with code that uses more than two levels deep with CAR or CDR.  For some reason (and it's just my luck), I cannot get code to work with CADDR or CADR or anything more than three characters, it always errors out on me.  I can borrow someone elses code from theSwamp, and I have to change it in order to get it to work for me.  I've just learned to accept it as 'my luck'... etc etc more waffle lol...

I'm sure you are joking here - but the beauty of programming is that everything is clean cut - the only reason it errors is because something you have entered is incorrect.. your setq statement errors because the the 'entget' will still be called when 'ssname' returns nil.

Quote from: Lee Mac
As for my point - think about how a COND statement evaluates when it reaches a test expression that returns true.

Code: [Select]
"%%129OPENPORT%%130"

Ahh, good point indeed.  When COND runs, it runs only once to complete it's task, then goes back to the beginning of the loop.  So if two fractions are in the same string, the second is then ignored and lost because the count then increases.  Ahh Nuts!

COND will evaluate the first statement in which the test expression returns true, then it will stop evaluating.
 
So, off the top of my head, I'm thinking after the first search of all text and an initial cleanup, do a second search of all text.  But what if there are three fractions in the same line of text ??  So that idea isn't going to work well.
There has to be a way to search the same string of text for more than one occurance of a fraction.

Yes, CAB has already shown you how to do it.

Hangman

  • Swamp Rat
  • Posts: 566
Re: Using 'wcmatch' ...
« Reply #16 on: June 07, 2010, 07:50:52 PM »
And then there's the issue with blocks that have these types of ASCI codes in them.
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Using 'wcmatch' ...
« Reply #17 on: June 07, 2010, 07:52:17 PM »
I suppose you'll have to dig through the block definitions too  :wink:

This should help you in that respect :-)

Code: [Select]
;; GetBlockText   by Lee McDonnell   [07.05.09]
;; ARGS: Blk   ~  Block Name [Str]
;; RETURN: List of Text Entities

(defun GetBlockText ( Blk )
  (if (tblsearch "BLOCK" Blk)
    (vl-remove-if-not
      (function
        (lambda ( e ) (wcmatch (cdr (assoc 0 (entget e))) "*TEXT"))
      )
      (GetSubObjs (tblobjname "BLOCK" Blk))
    )
  )
)

(defun GetSubObjs ( e )
  (if (setq e (entnext e)) (cons e (GetSubObjs e)))
)
« Last Edit: June 07, 2010, 07:59:25 PM by Lee Mac »

Hangman

  • Swamp Rat
  • Posts: 566
Re: Using 'wcmatch' ...
« Reply #18 on: June 08, 2010, 12:02:39 PM »
Nice code Alan  8-)

One thing I would add would be that vl-string-subst only replaces the first occurrence of the string to be replaced, hence I would usually use a function like:

Code: [Select]
(defun StringSubst ( new old str )
  ;; © Lee Mac  ~  23.05.10
  (
    (lambda ( i / nl ) (setq nl (strlen new))
      (while
        (and (< i (strlen str))
          (setq i (vl-string-search old str i))
          (setq str (vl-string-subst new old str i) i (+ i nl))
        )
      )
      str
    )
    0
  )
)

In place of it  :-)

Well, ... Perhaps it is another episode of 'Hangman's Luck', but I don't think this is necessarily the case for this purpose.

I went and re-wrote Alan's code (trying to learn it) using my own variables and such (minor changes, but I'm grasping the concept of his format - very nice by the way), and ran the program.  The program cleaned up the ASCii fractions nicely.  So out of curiosity, I went and placed several ASCii fractions into one string of text and ran the code again.  It cleaned up every one of them.  So I then pulled his code from here and ran it as-is, and it cleaned up the line of text with several ASCii fractions in it.

Now from the help file we read:
Quote
Note that the search is case-sensitive, and that vl-string-subst substitutes only the first occurrence it finds of the string.
So Lee is correct, it will only substitute the first occurance.

But then why is Alan's first code post above (without the update) able to clean up multiple occurances ??  At least on my machine it's cleaning up multiple occurances within the same line of text.
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Using 'wcmatch' ...
« Reply #19 on: June 08, 2010, 12:17:43 PM »
Try Alan's first code on something like:

Code: [Select]
%%130Lee%%129Mac%%130
Bear in mind that both codes are called with the same syntax, so you will need to make sure you load them before running.

Hangman

  • Swamp Rat
  • Posts: 566
Re: Using 'wcmatch' ...
« Reply #20 on: June 08, 2010, 01:23:48 PM »
Try Alan's first code on something like:
Code: [Select]
%%130Lee%%129Mac%%130Bear in mind that both codes are called with the same syntax, so you will need to make sure you load them before running.

Now this is where I get irritated.
Here's the result of your sample:
Code: [Select]
1/4Lee 1/2Mac‚
Which is:
 1/4Lee 1/2Mac%%130

And here are others I tried:
Code: [Select]
Dtext edit                 Actual text                         Results
ƒ"C, 1-2/C#18 ‚  =  %%131"C, 1-2/C#18%%129 %%130  ==>   3/4"C, 1-2/C#18 1/2  1/4

ƒ"C, "1-2/C#18†" =  %%131"C, %%129"1-2/C#18%%134"  ==>  3/4"C,  1/2"1-2/C#18 5/8"

ƒ"C, 1-2/C#18…   =  %%131"C, 1-2%%129/C#18%%133   ==>    3/4"C, 1-2 1/2/C#18 3/8

Now why is that ??  Could it be because the fraction at the end in my example is after a number and not a letter or word ??
 ...
Doht !!  I'm an idiot !!  It's catching EACH ascii fraction only once, so if I have twenty fractions, all different, in the same line of text, it'll pick them EACH up - but only once.

Gotcha !!  Now I see the light.  ggrrrrrrrr
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lee Mac

  • Seagull
  • Posts: 12921
  • London, England
Re: Using 'wcmatch' ...
« Reply #21 on: June 08, 2010, 01:59:52 PM »
You got it  :wink: