Re: Ascan with a Substr



On Mon, 05 Nov 2007 06:11:27 -0000, diogenes <jjaeckel@xxxxxxxxx> wrote:

Hello Diogenes and Johan

Thanks for your help. The following solution work well

if Ascan(aFiltre, { |x| SubStr(x,2,3) == AGENDA->KST }) = 0

Regards

Otto



On Nov 2, 4:27 am, otto <oha...@xxxxxxxxxxx> wrote:
Hello

How do I have to code a Ascan() with a SubStr() function ?

I like to see if the Substr(pos 2, Lenght 3) of AGENDA->KST is already in the array aFiltre.

This does not wordk !

if Ascan(aFiltre, { |x| SubStr(x,2,3) == AGENDA->KST })
Aadd(aFiltre, " " + AGENDA->KST + " " + AGENDA->JOB)
end

Thanks in advance for any help.
Otto

WHOOPS! Twice in a row now I accidentally clicked
on SEND before I meant to, thus sending you
incomplete responses. Sorry.

One more time:

Ok, here's my two bytes worth.
aScan(...) returns a NUMBER, so your code
needs to work with it that way. Hence,
the above suggestion:

if Ascan( aFiltre, SubStr(AGENDA->KST,2,3) ) <> 0

Aadd(aFiltre, " " + AGENDA->KST + " " + AGENDA->JOB)
end

comes closest to the mark. Myself, I would stick
with the code block so you can use the ==
operator to compare the strings. I think the
code block you originally used was just right,
assuming that AGENDA->KST is exactly a two-character
field.
But note: aScan(...) will return ZERO if the
string was NOT found, and non-zero if the string
IS found. So you should have:

if Ascan(aFiltre, { |x| SubStr(x,2,3) == AGENDA->KST }) = 0
Aadd(aFiltre, " " + AGENDA->KST + " " + AGENDA->JOB)
end

Note the = 0 test, rather than <> 0 test.
And note, you don't need to pre-initialize the
aFiltre array with something in it like somebody
else suggested. Just pre-init it with array(0)
(empty array) and I think that will work just fine.

Note on aScan(...) and the == comparison operator:
If you don't use a code block, the manual says
aScan(...) compares strings with the = operator,
which works differently depending on whether you
have EXACT set to be ON or OFF. Where I used to
work (among heavy-duty Clipper app programmers),
I found a distressing lack of in-depth understanding
among the staff about how = vs. == really works,
and worse still about how the other comparison
operators like > < >= and <= and <> really work
with EXACT ON vs. OFF. It's not discussed well
in any manual I've ever seen, but I experimented
with it in detail once until I figured it all out.

In your case, if you are comparing two strings
that are EXACTLY the same length (two characters
each), then you would have no problem. But my
general knee-jerk habit has always been to compare
strings with == instead of = unless I specifically
need to do otherwise, and to do that with aScan(...)
you need to use a code block. So stick to your
guns and do it that way.

My suggested code, above, is UN-tested just like
everybody else's. Hope it works for you if you
don't have your solution alrady, or if not, hope
the above doctoral dissertation gives you some
good clues how to deal with it.

There. NOW I can go ahead and click on Send.

-- Jay (diogenes) Jaeckel
---------------------------------------------
.



Relevant Pages

  • Re: Ascan with a Substr
    ... the above suggestion: ... operator to compare the strings. ... code block you originally used was just right, ...
    (comp.lang.clipper)
  • Re: A note on computing thugs and coding bums
    ... Here's my response including a bug fix. ... to make "modern strings" possible was designed and first implemented ... contents to strings, to compare them, and to duplicate them. ... while the Pike code will NEVER work...for international strings. ...
    (comp.programming)
  • Re: almost equal strings
    ... Those are good for comparing words, or arbitrary strings, as Roedy asked, but he reveals that he actually wants to compare phrases. ... You might need a more refined scoring method, though - not all insertion, deletions or substitutions are of equal importance. ... Anyway, i don't think cljp is the place to ask about this - there is a huge body of knowlege about text processing which Roedy could tap into, and there must be a place to ask questions on it. ...
    (comp.lang.java.programmer)
  • Re: Fastcode CompareStr B&V 1.1
    ... I noticed the special case that Pierre has optimized for too. ... Then you can compare first chars without reading the ... Many of the strings in the benchmark are different in the ... > missed this possible optimization when I wrote my functions;-) ...
    (borland.public.delphi.language.basm)
  • Re: Fast and Safe C Strings: User friendly C macros to Declare and use C Strings.
    ... Explain how such strings will improve the speed of strchr. ... If comparison fails, compare value against "needle" ... And these of course are just buffer overflows waiting to happen anyways. ... comparison of _one_ character: 'a' is not equivalent to 'f', ...
    (comp.lang.c)