I would like to split byte strings, for example AAFF10DC, with spaces, so it becomes AA FF 10 DC.
How to do this in AutoIt (v3)?
I would like to split byte strings … with spaces …
Example using StringRegExpReplace() :
Global Const $g_sString = 'AAFF10DC'
Global Const $g_sPattern = '(.{2})'
Global Const $g_sReplace = '$1 '
Global Const $g_sResult = StringRegExpReplace($g_sString, $g_sPattern, $g_sReplace)
ConsoleWrite($g_sResult & #CRLF)
Returns AA FF 10 DC.
This is sorta ugly, but it works:
$string = "AAFF10DC"
$strArray = StringSplit($string, "") ; No delimiter will separate all chars.
$strResult = ""
If IsEvenNumber($strArray[0]) Then
For $i = 1 to $strArray[0] Step 2
$strResult = $strResult & $strArray[$i] & $strArray[$i+1] & " "
Next
MsgBox(0, "Result", $strResult)
Else
MsgBox(0, "Result", "String does not contain an even number of characters.")
EndIf
Func IsEvenNumber($num)
Return Mod($num, 2) = 0
EndFunc
Global $s_string = "AAFF10DC"
MsgBox(64, "Info", _str_bytesep($s_string))
Func _str_bytesep($s_str, $s_delim = " ")
If Not (Mod(StringLen($s_str), 2) = 0) Then Return SetError(1, 0, "")
Return StringRegExpReplace($s_str, "(..(?!\z))", "$1" & $s_delim & "")
EndFunc
Is just another way to do it. For huge amounts of byte data, I would not suggest using this method.
Related
I have to import an Excel file into an Access db. My Excel has a column called 'Description'. Usually the description is long a cell, but it can happens that is more long. If it is long more than 255 characters, I cut the string and I modify last 3 characters with '...'.
When I run the program, though, I have an error "Index and length must refer to a location within the string".
I tried even to erase the check on the length, but obviously I had this error "Field is too small to accept the amount of data you attempted to add"
If flag = 2 Then
stringVoice = grid(r, 3).Text
voc.Description = voc.Description & stringVoice
If voc.Description.Length > 255 Then
voc.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
End If
End If
This is the code that produced the Index and length error. Same check has done before if inside a single cell there are more than 255 characters
Case 1 'voce
id = id + 1
flag = 2
voc = New cVoce
c_Voc = voc.Cod_Chapter
p_Voc = voc.Cod_Paragraph
voc.Cod_Chapter = grid(r, 1).Text.Substring(0, 1)
voc.Cod_Paragraph = grid(r, 1).Text.Split(".")(0)
voc.Cod_Voice = Right(vett(0), 2)
If grid(r, 3).Text.Length > 255 Then
voc.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
voc.Description = grid(r, 3).Text.ToString
If voc.Description.EndsWith("-") Then
a = Replace(voc.Description, "-", "")
voc.Description = a
End If
End If
stringVoice = voc.Description
voices.Add(voc)
but in this case I've never had an error.
Can anyone help me to fix my code?
I have a long text which needs to be converted to small strings so I can include it to an AutoIt script. If I include multi-line text it shows error unterminated string. So I should have:
"numbercharswillbe10" &_ "othernumbersofcharwillbe10" &_ etc..
How can I split it with & _ -delimiters?
String concatenation
As per Documentation - Language Reference - Operators:
& Concatenates/joins two strings.
&= Concatenation assignment.
Example:
Global $g_sText = "Long " & "string " & "here." & #CRLF
$g_sText &= "More text." & #CRLF
ConsoleWrite($g_sText)
Multi line statements
As per Documentation - Language Reference - Comments (emphasis added, as it causes mentioned "unterminated string" error):
Although only one statement per line is allowed, a long statement can span multiple lines if an underscore "_" preceded by a blank is placed at the end of a "broken" line. String definition cannot be split in several lines, concatenation need to be used.
Example:
Global Const $g_sText = "Long " & _
"string " & _
"here." & _
#CRLF & _
"More text." & _
#CRLF
ConsoleWrite($g_sText)
Double-quotes
As per Documentation - FAQ - Double quotes:
If you want to use double-quotes inside a string then you must "double them up". So for every one quote you want you should use two. ...
or use single quotes instead ...
Examples available from source.
Defaults and limits
As per Documentation - Appendix - Limits/defaults:
4095 Maximum size for a line of script.
2,147,483,647 Maximum string length.
As per Documentation - Language Reference - Datatypes - Strings:
All AutoIt strings use UTF-16 (in fact and more precisely UCS-2) encoding.
As per Documentation - Intro - Unicode Support:
There are a few parts of AutoIt that don't yet have full Unicode support. These are:
Send and ControlSend - Instead, Use ControlSetText or the Clipboard functions.
Console operations are converted to ANSI.
Alternatives
Alternatives to hard coding include ClipGet() and FileRead().
Text from clipboard
Example (select and copy text CTRL + C first):
Global Const $g_sText = ClipGet()
ConsoleWrite($g_sText & #CRLF)
Text from file
Example (create C:\my_long_string.txt first):
#include <FileConstants.au3>
Global Const $g_sFile = 'C:\my_long_string.txt'
Global Const $g_sText = _TextFromFile($g_sFile)
ConsoleWrite($g_sText & #CRLF)
Func _TextFromFile(Const $sFile)
Local $hFile = FileOpen($sFile, $FO_READ + $FO_UTF8_NOBOM)
Local Const $sData = FileRead($hFile)
FileClose($hFile)
Return $sData
EndFunc
Split string
Alternatives to hard coded manual string splitting include StringSplit(), _StringExplode() (related) and StringMid().
Structural
StringSplit() splits a string into array of:
individual characters (on empty delimiter),
words (on space delimiter) or
lines (on #CRLF, #LF or #CR delimiter).
Equal length
StringMid() returns part of a string. Can be used to split into parts of equal length. Example (no error checking, select and copy text CTRL + C first):
#include <Array.au3>
Global Const $g_iSize = 10
Global Const $g_sText = ClipGet()
Global Const $g_aArray = _StringSplitEqual($g_sText, $g_iSize)
_ArrayDisplay($g_aArray)
Func _StringSplitEqual(Const $sText, Const $iSize = 1)
Local Const $iLength = StringLen($sText)
Local Const $iParts = Ceiling($iLength / $iSize)
Local Const $iRest = -1; $iLength - ($iSize * Floor($iLength / $iSize))
Local $iStart = 0
Local $iCount = 0
Local $aArray[$iParts]
For $i1 = 0 To $iParts - 1
$iStart = ($i1 * $iSize) + 1
$iCount = ($i1 < $iParts - 1) ? $iSize : ($iRest ? $iRest : $iSize)
$aArray[$i1] = StringMid($sText, $iStart, $iCount)
Next
Return $aArray
EndFunc
Join string
As per documentation:
_ArrayToStringPlaces the elements of a 1D or 2D array into a single string, separated by the specified delimiters
Example (add _StringSplitEqual() and select and copy text CTRL + C first):
#include <Array.au3>
Global Const $g_iSize = 10
Global Const $g_sStart = '$sText = "'
Global Const $g_sEnd = '"' & #CRLF
Global Const $g_sDelimiter = '" _' & #CRLF & ' & "'
Global Const $g_sText = StringReplace(ClipGet(), #CRLF, '')
Global Const $g_aArray = _StringSplitEqual($g_sText, $g_iSize)
Global $g_sResult = _ArrayToString($g_aArray, $g_sDelimiter)
$g_sResult = $g_sStart & $g_sResult & $g_sEnd
ConsoleWrite($g_sResult)
Returns:
$sText = "AutoIt v3 " _
& "is a freew" _
& "are BASIC-" _
& "like scrip" _
& "ting langu" _
& "age design" _
& "ed for aut" _
& "omating th" _
& "e Windows " _
& "GUI and ge" _
& "neral scri" _
& "pting."
Hello everyone.
Dim txt1 As Double = Convert.ToDouble(TextBox1.Text) / 100
Dim txt2 As Double = Convert.ToDouble(TextBox2.Text)
Dim txt3 As Double = Convert.ToDouble(TextBox3.Text)
Dim txtResult As Double = Convert.ToDouble(TextBox4.Text)
Dim result As Double = txt1 * txt2 * txt3
TextBox4.Text = result
As you can see I get my result depending on what the user types in. So I have to add a space after a certain character. Textbox14.text(0) <--- after this do I want my space. It's so that after the value is higher than 999 it should type out 1 000 and not 1000. Thank you very much for any useful help, I've truly looked everywhere, I just can't find anything.
You talking about group separator. Custom Numeric Format Strings
You can use .ToString() method and define group separator in the format.
TextBox4.Text = result.ToString("0,0.000")
Different separators will be used based on the local system's language/region settings.
You can define your custome separator manually
var cultureInfo = new System.Globalization.CultureInfo("en-US");
var numberInfo = cultureInfo.NumberFormat;
numberInfo.NumberGroupSeparator = " ";
TextBox4.Text = result.ToString("0,0.000", numberInfo)
If I get it right, you want every 3 chars a space, right ?
Like 1 000 000 ?
try this :
Dim result As String, str As String, ret As String
Dim i As Integer
Dim arr As Char()
'your text to space
result = "10000000"
'reverte so we start with the end
result = StrReverse(result)
i = 0
ret = ""
' make a char array which each char is an own array element
arr = result.Take(result.Length).ToArray
'iterate through all elements
For Each str In arr
' skip the first element .
' only add a space every 3 elements
If (i <> 0) And (i Mod 3 = 0) Then
ret = ret + " "
End If
ret = ret + str
i = i + 1
Next
' revers again the output
ret = StrReverse(ret)
MsgBox(ret)
So I have the following code to split a string between whitespaces:
text = "I am 'the text'"
for string in text:gmatch("%S+") do
print(string)
end
The result:
I
am
'the
text'
But I need to do this:
I
am
the text --[[yep, without the quotes]]
How can I do this?
Edit: just to complement the question, the idea is to pass parameters from a program to another program. Here is the pull request that I am working, currently in review: https://github.com/mpv-player/mpv/pull/1619
There may be ways to do this with clever parsing, but an alternative way may be to keep track of a simple state and merge fragments based on detection of quoted fragments. Something like this may work:
local text = [[I "am" 'the text' and "some more text with '" and "escaped \" text"]]
local spat, epat, buf, quoted = [=[^(['"])]=], [=[(['"])$]=]
for str in text:gmatch("%S+") do
local squoted = str:match(spat)
local equoted = str:match(epat)
local escaped = str:match([=[(\*)['"]$]=])
if squoted and not quoted and not equoted then
buf, quoted = str, squoted
elseif buf and equoted == quoted and #escaped % 2 == 0 then
str, buf, quoted = buf .. ' ' .. str, nil, nil
elseif buf then
buf = buf .. ' ' .. str
end
if not buf then print((str:gsub(spat,""):gsub(epat,""))) end
end
if buf then print("Missing matching quote for "..buf) end
This will print:
I
am
the text
and
some more text with '
and
escaped \" text
Updated to handle mixed and escaped quotes. Updated to remove quotes. Updated to handle quoted words.
Try this:
text = [[I am 'the text' and '' here is "another text in quotes" and this is the end]]
local e = 0
while true do
local b = e+1
b = text:find("%S",b)
if b==nil then break end
if text:sub(b,b)=="'" then
e = text:find("'",b+1)
b = b+1
elseif text:sub(b,b)=='"' then
e = text:find('"',b+1)
b = b+1
else
e = text:find("%s",b+1)
end
if e==nil then e=#text+1 end
print("["..text:sub(b,e-1).."]")
end
Lua Patterns aren't powerful to handle this task properly. Here is an LPeg solution adapted from the Lua Lexer. It handles both single and double quotes.
local lpeg = require 'lpeg'
local P, S, C, Cc, Ct = lpeg.P, lpeg.S, lpeg.C, lpeg.Cc, lpeg.Ct
local function token(id, patt) return Ct(Cc(id) * C(patt)) end
local singleq = P "'" * ((1 - S "'\r\n\f\\") + (P '\\' * 1)) ^ 0 * "'"
local doubleq = P '"' * ((1 - S '"\r\n\f\\') + (P '\\' * 1)) ^ 0 * '"'
local white = token('whitespace', S('\r\n\f\t ')^1)
local word = token('word', (1 - S("' \r\n\f\t\""))^1)
local string = token('string', singleq + doubleq)
local tokens = Ct((string + white + word) ^ 0)
input = [["This is a string" 'another string' these are words]]
for _, tok in ipairs(lpeg.match(tokens, input)) do
if tok[1] ~= "whitespace" then
if tok[1] == "string" then
print(tok[2]:sub(2,-2)) -- cut off quotes
else
print(tok[2])
end
end
end
Output:
This is a string
another string
these
are
words
I'm having to check data entry on an address field. The client does not want users to use terms like Rd. or Rd for road, ave or ave. for avenue etc. I have no problem with most of the terms. Where I have issues is with 'Ave' lets say. If I look for ' AVE ', that's fine but it will not pick up on ' AVE' at the end of the string and if I look for ' AVE' it will get a false positive on ' Avenue' since it will find ' Ave' within that string. Anyone have an idea of how I can go about this?
Thank you for any help.
Norst
Although the Q: is not language specific, here is how I'm going about this in JS:
//function to check address for rd rd. ave ave. st st. etc
function checkaddy() {
//array of items to look for
var watchfor = Array();
watchfor[0] = " RD";
watchfor[1] = " RD.";
watchfor[2] = " AVE ";
watchfor[3] = " AVE.";
watchfor[4] = " ST ";
watchfor[5] = " ST.";
watchfor[6] = " BLVD.";
watchfor[7] = " CRT ";
watchfor[8] = " CRT.";
watchfor[9] = " CRES ";
watchfor[10] = " CRES.";
watchfor[11] = " E ";
watchfor[12] = " E.";
watchfor[13] = " W ";
watchfor[14] = " W.";
watchfor[15] = " N ";
watchfor[16] = " N.";
watchfor[17] = " S ";
watchfor[18] = " S.";
watchfor[19] = " PKWY ";
watchfor[20] = " PKWY.";
watchfor[21] = " DR ";
watchfor[22] = " DR.";
//get what the user has in the address box
var addcheck = $("#address").val();
//upper case the address to check
addcheck = addcheck.toUpperCase();
//check to see if any of these terms are in our address
watchfor.forEach(function(i) {
if (addcheck.search(watchfor[i]) > 0 ) {
alert("Found One!");
}
});
}
Perhaps you need to look for word boundary character \b. Here are some Ruby examples:
irb(main):002:0> " Avenue" =~ / AVE\b/i
=> nil
irb(main):003:0> " Ave" =~ / AVE\b/i
=> 0
irb(main):005:0> " Ave" =~ /\bAVE\b/i
=> 1
irb(main):006:0> " Ave" =~ /\bAVE\b/i
Notice how " Avenue" doesn't match while " AVE" does match. Also notice how the '\b' behaves and we get 0 and 1 respectively.
There are other characters classes as well in regular expressions. So all you need to do is formulate correct REs for your problem set.
I hope that helps.
Why would your client insist on spelling out names? The United States Postal Service actually encourages abbreviations. Not only that, they prefer addresses to be in all uppercase letters and no more than 5 lines. Such specifications are what their automated sorters were built for. But I digress.
To actually answer your question, you may consider the following code. There was a mistake in your forEach declaration. You were using i as an index, but, in fact, the forEach function uses the whole entry of the array. I modified it below. Also, because we're using a string expression in the constructor for the RegExp, the \ in the \b has to be escaped, so we add two \'s inside the string. Because we using the \b construct for word boundaries, we don't need to add extra periods into the test array. I hope you find this helpful.
//array of items to look for
var watchfor = ['RD','AVE','ST','BLVD','CRT','CRES','E','W','N','S','PKWY','DR'];
//function to check address for rd rd. ave ave. st st. etc
function checkaddy(address) {
//check to see if any of these terms are in our address
watchfor.forEach(function(entry) {
var patt1 = RegExp('.*\\b' + entry + '\\b.*','gim');
if (patt1.test(address)) {
document.write("Found " + entry);
}
});
}