simy8891 Posted November 24, 2013 Share Posted November 24, 2013 Hi guys, I'm working at making a ZPL template, which is gonna print also a Bar Code (Code 128). Now, I'd like to optimize this code rather than using Subset B (Ascii) everytime. To make it short, if I have this string: WT0F525M23112013001 It needs to become like this: :WT0F525M>523112013001 If I have this string: 1234567WT0F525M231120130A01 It'll be: ;123456>6WT0F525M>523112013>60A01 These are the outputs in order to have an optimised code 128 bar code. ":" starts the subset B at the beginning of the string. If there's a number greater (or same) than 4 digits, it'll be ";" which starts subset C. Subset C can only work with pair digits (so every 2) and it's useless to be used with less than 4. So with this rules in mind, is there any suggestion on the code I can write to have this happening? Is there a way to split the string in arrays of numbers and letters? I think this will help me with controlling parts by parts For instance WT0F525M23112013001, I'd like to see: $array[0] -> Counter of arrays $array[1] -> WT $array[2] -> 0 $array[3] -> F $array[4] -> 525 $array[5] -> M $array[6] -> 231120130001 So now I could just check if it begins with a letter or with a number, and if it's a number, check its lenght, if same or greater than 4 add ";" at the beginning on the new string, then check if it's odd or even, if it's odd, add <6 and the last number afterwards in the new string then "Next" to the other container and so on.. Sorry for the confusion, but I'm confused myself.. I know how it works, I'm just trying to keep the code as smaller as possible and avoid useless operations. Thanks Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 24, 2013 Moderators Share Posted November 24, 2013 simy8891,Splitting the string is easy: #include <Array.au3> $sString = "WT0F525M23112013001" $aSplit = StringRegExp($sString, "\d+|\D+", 3) _ArrayDisplay($aSplit)But I got completely lost in the explanation of how to reconstitute the string from then on! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
simy8891 Posted November 25, 2013 Author Share Posted November 25, 2013 Cool! I still don't understand how StringRegExp works though I ran into this function before posting this, but I really didn't get it. Anyway I tried it and it splits the string the way I wanted. This will make the checks way easier. Thanks! Link to comment Share on other sites More sharing options...
jchd Posted November 25, 2013 Share Posted November 25, 2013 I believe it can be made more practical. The regexp can be tuned so as to only split groups of pairs of digits with a minimal repetition of 3 (hence at least 6 digits in a row, since 5 or less don't justify going to subset C). Furthermore, you can perform the full packing in a small function: #include <Array.au3> Local $sStringA = "WT0F525M23112013001XX12345YYY5555555555" Local $sStringN = "1234567WT0F525M231120130A01" Func _Pack128($sPlainCode) Local $sC128BC = ":" $sC128BC &= StringRegExpReplace($sPlainCode, "((?:\d\d){3,})", ">6\1>5") $sC128BC = StringRegExpReplace($sC128BC, "^:>6", ";") $sC128BC = StringRegExpReplace($sC128BC, ">5$", "") Return($sC128BC) EndFunc ConsoleWrite(_Pack128($sStringA) & @LF) ConsoleWrite(_Pack128($sStringN) & @LF) What we do here is unconditionally replace strings of 3 or more pairs of digits by the packing sequence. Then clean up the possible parasitic ">5" at the tail if the input ended with such a sequence, and similarly adjust the head of the result in case we have alpha (or too short a digit sequence) ahead of the input. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
simy8891 Posted November 25, 2013 Author Share Posted November 25, 2013 (edited) WOW. That's short and works great!! Just a thing, I had to replace this ">61>5" with this ">51>6" since your code was switching to subset b rather than c and viceversa, still great! I actually managed to finish my own script a few minutes ago and it's like huge.. I really need to understand how this StringRegExp works. Another thing, now I get some problems with the formatting: when a string finished in subset C, the code keeps adding >6 at the end of the string. Is there a way to clean this up in a better way, rather than having 2 If or a Switch Case to remove >5/>6? This was the code I just finished.. I'm a bit embarrassed to compare it with yours.. expandcollapse popup#include <Array.au3> $SN="WT0F525M291129138326" $aSplit=StringRegExp($SN, "\d+|\D+", 3) $aLenght=UBound($aSplit)-1 $SN_Code128='>' _ArrayDisplay($aSplit) ;***************** Subset Control Flag *************************************************************** ;Set a control flag in order to keep track of what Subset is in use ;1=Subset B ;2=Subset C $ControlFlag=1 ;***************** FIRST STRING *************************************************************** $FirstString=StringLen($aSplit[0]) If StringIsDigit($aSplit[0]) And $FirstString>3 Then ;Start in Code C $SN_Code128=$SN_Code128&';' If IsEven($FirstString) Then ;Number is even, so it'll all be in Code C $SN_Code128=$SN_Code128&$aSplit[0] $ControlFlag=2 Else ;Not even, so the last number has to be written in Code B $SN_Code128=$SN_Code128&StringTrimRight($aSplit[0],1) $SN_Code128=$SN_Code128&'>6'&StringTrimLeft($aSplit[0],$FirstString-1) $ControlFlag=1 EndIf Else ;Start in Code B (ASCII) $SN_Code128=$SN_Code128&':'&$aSplit[0] $ControlFlag=1 EndIf ;******************** REST OF THE STRINGS ******************************************************** ;Check if there are actually more strings If $aLenght > 1 Then For $i=1 to $aLenght ;Starts from #1 since #0 has been processed before $StringLenght=StringLen($aSplit[$i]) If StringIsDigit($aSplit[$i]) And $StringLenght>3 Then ;It needs code C, so first check the Control flag, if it's already in code C, it won't add any subset change If $ControlFlag=1 Then $SN_Code128=$SN_Code128&'>5' $ControlFlag=2 EndIf If IsEven($StringLenght) Then ;Number is even, so it'll all be in Code C $SN_Code128=$SN_Code128&$aSplit[$i] $ControlFlag=2 Else ;Not even, so the last number has to be written in Code B $SN_Code128=$SN_Code128&StringTrimRight($aSplit[$i],1) $SN_Code128=$SN_Code128&'>6'&StringTrimLeft($aSplit[$i],$StringLenght-1) $ControlFlag=1 EndIf Else ;It needs code B, so first check the Control flag, if it's already in code B, it won't add any subset change If $ControlFlag=2 Then $SN_Code128=$SN_Code128&'>6' $ControlFlag=1 EndIf $SN_Code128=$SN_Code128&$aSplit[$i] $ControlFlag=1 EndIf Next EndIf MsgBox(0,'i',$SN_Code128) ; ********** FUNCTIONS *********************** ; IS EVEN Func IsEven($number) Return ($number/2)=Round($number/2) EndFunc Edited November 25, 2013 by simy8891 Link to comment Share on other sites More sharing options...
Solution jchd Posted November 25, 2013 Solution Share Posted November 25, 2013 Oops, sorry about the mistakes: I actually switched ">5" and ">6" everywhere. But at least you get the idea. Please try this fix and report if it doesn't work for some case: Local $sStringA = "WT0F525M23112013001XX12345YYY5555555555" Local $sStringN = "1234567WT0F525M231120130A01" Func _Pack128($sPlainCode) Local $sC128BC = ":" $sC128BC &= StringRegExpReplace($sPlainCode, "((?:\d\d){3,})", ">5\1>6") $sC128BC = StringRegExpReplace($sC128BC, "^:>5", ";") $sC128BC = StringRegExpReplace($sC128BC, ">6$", "") Return($sC128BC) EndFunc ConsoleWrite(_Pack128($sStringA) & @LF) ConsoleWrite(_Pack128($sStringN) & @LF) I'm afraid I don't have a simpler way to clean up the result. simy8891 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
simy8891 Posted November 25, 2013 Author Share Posted November 25, 2013 It works perfectly. I was so proud of my code before you showed up Thanks man, I really appreciate the help, is there a guide how to use the StringRegExp set of functions? I've checked the online function help, but it really don't say much and I still don't understand it correctly Link to comment Share on other sites More sharing options...
jchd Posted November 25, 2013 Share Posted November 25, 2013 (edited) Have a look at the current beta version of AutoIt: the StringRegExp help there has been greatly improved. If you don't want to install the beta (you probably should anyway) you can still have a free peep at it here. Disclaimer: this is my copy and it may not have the latest typos fixed. Anyway 99.99% of it is correct unless I goofed (again). My signature carries some links to regexp primers and fairly advanced tutorials. Edited November 25, 2013 by jchd This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now