Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
String to Char Array...
Message
From
04/11/2000 06:21:38
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
03/11/2000 14:17:39
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00437794
Message ID:
00437989
Views:
19
>Hello to all!
>
>I'm a Java and a C programmer so I'm used to seeing strings as character arrays. I was wondering if the same thing existed in FoxPro?
>
>Basically, if I try to explain what I'm trying to do, I want to compare two strings together and return a percentage that represents how identical they both are. For this, I have to compare each of the two words letter by letter. I haven't found a better way to do this than by using the function SUBSTR() inside a FOR loop. I think that this method is not optimal (there MUST be a better way to return 1 character from a string at a specified position than SUBSTR()!).
>
>Anyway, I must use FoxPro for this because of how fast it is with text. So I sure hope I'll find a way to run this application that'll exploit FoxPro's strengths.
>
>Thanks a lot in advance, Stephane.

Stephane,
For comparing there are soundex() and difference().
You're right on that C style char array is most powerfull way in processing strings. You could make a string into an array in a lot of ways. One typical is substr() function which is rather slow for this. Another one is lowlevel processing of string, which may at first seem silly but much faster than substr(). And another one which I do for similar purposes is to create a FLL in C. Here is C code for FLL (pls be notified that my C knowledge is very limited, while this works you could optimize it) :
#include <pro_ext.h>

char FAR *NullTerminate(Value FAR *cVal)
{
	char *RetVal;
	if (!_SetHandSize(cVal->ev_handle, cVal->ev_length + 1))
	{
		_Error(182); // "Insufficient memory"
	}
	
	((char FAR *) _HandToPtr(cVal->ev_handle))[cVal->ev_length] = '\0';
	RetVal = (char FAR *) _HandToPtr(cVal->ev_handle);
	return RetVal; 
}

void FAR Str2Array(ParamBlk FAR *parm)
{
	Value val;

	char FAR *InString = NullTerminate(&parm->p[0].val);	
	char FAR *ArrayName = NullTerminate(&parm->p[1].val);
	int Slen = _StrLen(InString);
	
	Locator loc;
	NTI nti;
	if ( (nti = _NameTableIndex(ArrayName)) != -1 )
	{
		if ( _FindVar(nti, -1, &loc) )         // If exists - release
			_Release(nti);
	}
	loc.l_subs = 1;
	loc.l_sub1 = Slen;
	loc.l_sub2 = 0;
	_NewVar(ArrayName,&loc,NV_PUBLIC); // Create one dimension public array

    val.ev_type = 'C';
	val.ev_length = 1;
	val.ev_handle = _AllocHand(2);
	char FAR *rep = (char * ) _HandToPtr(val.ev_handle);
	_HLock(val.ev_handle);
	int i=0;
	while(InString[i] != '\0')
	{
		loc.l_sub1 = i+1; // Set next array elem - FP arrays start at 1
		
		rep[0] = InString[i++] ;     // Get current char
		rep[1] = '\0';  // null terminate

		_Store(&loc, &val); // Store to array elem
	}
	_HUnLock(val.ev_handle);
	_FreeHand(val.ev_handle);
	_RetInt(Slen,10); // return array len - string length
}

FoxInfo myFoxInfo[] =
{
	{"ASTRING", (FPFI) Str2Array, 2, "CC"},
};


extern "C" {
FoxTable _FoxTable =
{
	(FoxTable *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};
}
Sample usage from foxpro :
set library to str2arr.fll
lnLen1 = str2arr('This is string1.','aTest1')
lnLen2 = str2arr('This is string2. Up to 65000 chars could be used.','aTest2')
disp memo like aTest1
disp memo like aTest2
"Chapter 28: Accessing the Visual FoxPro API" in online help details how you'd build the FLL.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform