Join us today!

Find 5 strings with...
 
Notifications
Clear all

Find 5 strings with one fonction

7 Posts
4 Users
3 Reactions
547 Views
Posts: 2
Topic starter
(@idat90)
New Member
Joined: 1 year ago

I need fonction or méthode  tips for verify New string with other 5 strings 

Reply
6 Replies
1 Reply
(@fisothemes)
Joined: 1 year ago

Active Member
Posts: 6

Simplest and fasted way to do this is:

FUNCTION F_ContainsString : BOOL
VAR_IN_OUT
	StringArray : ARRAY [*] OF T_MaxString;
END_VAR
VAR_IN_OUT CONSTANT
	Value : T_MaxString;
END_VAR
VAR
	i : DINT;
END_VAR

=============================================================

FOR i := LOWER_BOUND(StringArray, 1) TO UPPER_BOUND(StringArray,1) DO 
	F_ContainString := StringArray[i] = Value;
	IF F_ContainString THEN RETURN; END_IF
	END_FOR

I have also library called TwinCAT Dynamic Collections.

There's a data structure called a Set, it only stores unique values. You can use the hash set (FB_Hash_Set) or tree set (FB_Tree_Set) to store existing QR codes then check of they exist in the set. It's dynamic so you can store as many QR codes as you need for your application. Heck, you can use it to store all the QR codes in your inventory (as long as you have less than 2 billion items) at run-time and find then extremely fast.

 

Here's some example code:

PROGRAM MAIN
VAR CONSTANT
	QR_CODE_LENGTH : __UXINT := 4;
END_VAR
VAR
	eState : (E_INIT, E_RUN);
	bDoesQrCodeExist : BOOL;
	sNewQrCode : STRING(QR_CODE_LENGTH) := '0011';
	fbQrCodeSet : TcDynCollections.FB_Hash_Set(0);
	arsQrCodes : ARRAY[0..4] OF STRING(QR_CODE_LENGTH) := ['1001', '0110', '1101', '1110', '0101'];
	i : TcDynCollections.T_Position;
END_VAR
=============================================================
CASE eState OF
	E_INIT: // Add existing QR codes to set.
		FOR i := 0 TO 4 DO fbQrCodeSet.Insert(arsQrCodes[i]); END_FOR
		eState := E_RUN;
	E_RUN: // Check if QR Code is new by checking of it's contained in the set.
		bDoesQrCodeExist := fbQrCodeSet.Contains(sNewQrCode);	
END_CASE

 

You can even take advantage of a Map/Dictionary/Associative Array (FB_Hash_Map or FB_Tree_Map) in the library to store metadata along with your QR code in the sense that Key = QR Code and Value = Struct/Function Block containing metadata.

Reply
Posts: 30
 Alex
(@alex)
Eminent Member
Joined: 2 years ago

Hello, 
Perhaps I miss something, could you be a little more specific ?

Alex

 

Reply
Posts: 2
Topic starter
(@idat90)
New Member
Joined: 1 year ago

In the programme i receive a New QR CODE type String , and need to compare with 4 existing QR code , are same or not 

I need fonction or méthode can Do this tips liké fin(;)

Reply
Posts: 30
 Alex
(@alex)
Eminent Member
Joined: 2 years ago

Hello, It's just a function which do the job 🙂
If it's only 4/5 string  then :

FUNCTION StringCompare : BOOL
VAR_INPUT
	StrComp			:STRING(8);
END_VAR
VAR
	Str1			:STRING(8) :='01234567';
	Str2			:STRING(8) :='12345678';
	Str3			:STRING(8) :='23456789';
	Str4			:STRING(8) :='34567890';
	Str5			:STRING(8) :='45678901';
END_VAR

IF StrComp = str1 THEN
		StringCompare:=TRUE;
	ELSIF StrComp = str2 THEN
		StringCompare:=TRUE;
	ELSIF StrComp = str3 THEN
		StringCompare:=TRUE;
	ELSIF StrComp = str4 THEN
		StringCompare:=TRUE;
	ELSIF StrComp = str4 THEN
		StringCompare:=TRUE;
	ELSE
		StringCompare:=FALSE;
END_IF

In you main call the function :

IF Compare THEN
Compare:=FALSE;
Result := StringCompare(StrComp:=Str2comp);
END_IF

where Str2comp is your QR code

 

my 2 cts,

Reply
runtimevictor
Posts: 156
(@runtimevictor)
Estimable Member
Joined: 2 years ago

Hello Alex,

https://github.com/tcunit/TcUnit/issues/108

Reply
1 Reply
 Alex
(@alex)
Joined: 2 years ago

Eminent Member
Posts: 30

@runtimevictor Thanks, sounds better....

Reply
Share: