storing result of expect expression in variable - scripting

I'm trying to store the length of a string variable in another variable. If I open an expect prompt and run string length test, I get 4. However, if I try set len string length $var I get:
wrong # args: should be "set varName ?newValue?"
while executing
"set len string length $var"
I also tried set len {string length $var} but then the len variable simply contains the string "string length $var". What am I doing wrong?

Command substitution will happen only if you have enclosed them required commands within square brackets.
set var "CalumMcCall"
set len [string length $var]

Related

how to define a string variable with space in variable name in GAMS with $set

I would like to define a string variable in GAMS to call a server. Here is the syntax:
$set SERVER SERVER=ASERVER
The problem is that there is a space in the server name, so that the actual syntax is
$set SERVER SERVER=A SERVER
Then errors are reported like this "Error Unknow option "Server"". How should I handle a string variable with space in variable name? Thanks
Try it with quotes:
$set SERVER 'SERVER=A SERVER'
Edit: Another example how to use $call with an argument containing spaces (note that this is for Windows only, on Unix you would have to handle the spaces and quotes differently):
$echo $log %x% > log.gms
$call 'gams log.gms --x="With Space" lo=%gams.lo%'

Printing Unnecessary escape character [duplicate]

I tried many ways to get a single backslash from an executed (I don't mean an input from html).
I can get special characters as tab, new line and many others then escape them to \\t or \\n or \\(someother character) but I cannot get a single backslash when a non-special character is next to it.
I don't want something like:
str = "\apple"; // I want this, to return:
console.log(str); // \apple
and if I try to get character at 0 then I get a instead of \.
(See ES2015 update at the end of the answer.)
You've tagged your question both string and regex.
In JavaScript, the backslash has special meaning both in string literals and in regular expressions. If you want an actual backslash in the string or regex, you have to write two: \\.
The following string starts with one backslash, the first one you see in the literal is an escape character starting an escape sequence. The \\ escape sequence tells the parser to put a single backslash in the string:
var str = "\\I have one backslash";
The following regular expression will match a single backslash (not two); again, the first one you see in the literal is an escape character starting an escape sequence. The \\ escape sequence tells the parser to put a single backslash character in the regular expression pattern:
var rex = /\\/;
If you're using a string to create a regular expression (rather than using a regular expression literal as I did above), note that you're dealing with two levels: The string level, and the regular expression level. So to create a regular expression using a string that matches a single backslash, you end up using four:
// Matches *one* backslash
var rex = new RegExp("\\\\");
That's because first, you're writing a string literal, but you want to actually put backslashes in the resulting string, so you do that with \\ for each one backslash you want. But your regex also requires two \\ for every one real backslash you want, and so it needs to see two backslashes in the string. Hence, a total of four. This is one of the reasons I avoid using new RegExp(string) whenver I can; I get confused easily. :-)
ES2015 and ES2018 update
Fast-forward to 2015, and as Dolphin_Wood points out the new ES2015 standard gives us template literals, tag functions, and the String.raw function:
// Yes, this unlikely-looking syntax is actually valid ES2015
let str = String.raw`\apple`;
str ends up having the characters \, a, p, p, l, and e in it. Just be careful there are no ${ in your template literal, since ${ starts a substitution in a template literal. E.g.:
let foo = "bar";
let str = String.raw`\apple${foo}`;
...ends up being \applebar.
Try String.raw method:
str = String.raw`\apple` // "\apple"
Reference here: String.raw()
\ is an escape character, when followed by a non-special character it doesn't become a literal \. Instead, you have to double it \\.
console.log("\apple"); //-> "apple"
console.log("\\apple"); //-> "\apple"
There is no way to get the original, raw string definition or create a literal string without escape characters.
please try the below one it works for me and I'm getting the output with backslash
String sss="dfsdf\\dfds";
System.out.println(sss);

How could BRO-IDS compare strings with NUL-terminator

I am testing string comparison with BRO, and got some runtime errors. Hope you guys could take a look and give me some hints.
For example i have two strings, let's say str_A and str_B, str_A is sort of a pattern, like: str_A = "\x13\x02\xf0\x80";
And str_B is a payload(contents) string from the function:
event tcp_packet(c: connection, is_orig: bool, flags: string, seq: count, ack: count, len: count, contents: string)
I compared the two of the strings with: if(str_A in str_B), which reduced the runtime errors like:
1467860547.182543 error: string with embedded NUL: "\x13\x00\xf0\x13"
1467860547.182543 error: string without NUL terminator: "\x13\x00\xf0\x13\x02\xf0\x80\x02\x00\x00\xc0\x01\x00\x00\x00\x00\x87\x02"
It looks like the 'x00' in the middle of the pattern string was considered as a terminator, and for the latter there wasn't a NUL at the end of the str_B.
So the (silly) question is how i could append a NUL at the end of str_B within BRO? and how to make BRO ignore the embeded NUL in the middle of a string when comparing? Many Thanks.
This was figured all right by translating(calling the function string_to_ascii_hex()) the hex-string into an ASCII-hex-string.

Binary file output for fixed length string

I am trying to write a binary file which also has a string which i want to have as fixed length in vb.net. I tried lset, padleft in debug, the value returned is correct but in the output file, the first character before the string is the fixed length i specified. why does the binary writer write the additional char ?
I found out that if if you don't want or need the length byte you can call Write with a Char [] array instead of a String

VB.NET - string of nulls

I have a string value read in from a CSV file. The CSV file contains 7 NULL bytes, I have confirmed this by opening it in a hex editor and sure enought there are 7 0x0 bytes in there. This string is causing me pain.
In vb.net when I check the strlen of this string it returns a value of 7 and if i do a String.IsNullOrWhitespace it returns false.
I cannot understand why this is? I have split the string into a byte array and each byte is 0x0, which is null/nothing. A string = Nothing comparison also fails.
I want to be able to replace this string with a string of my own but I cannot do this dynamically. Any suggestions why this string returns a length of 7 even though each byte is 0x0?
Unfortunately the null character seven times is not an empty string, or a null string. Remember in .NET a string is at some level a pointer to a character array. A string is null if this pointer is set to null. A string is empty if the pointer points to a zero length array. In this case the pointer points to a length seven array of null characters (the byte being all zeros).
Null String
A ->
Empty String
A -> ()
Your String
A -> ((0)(0)(0)(0)(0)(0)(0))
You can test for this null character by using
char nullChar = char.ConvertFromUtf32(0);
string nullCharString = new String(nullChar);
bool hasNullChar = A.Contains(nullCharString);
The Null character is not whitespace, and your string reference is not Nothing, so I would expect String.IsNullOrWhitespace() to return false
A character with the character code zero is a character just like any other. If you have a string with seven such characters, the length is seven. The NUL character is not a white-space character, and a string containing NUL characters is not the same as a string reference that is null (Nothing).
You could use the Trim method (or TrimEnd) to remove the NUL characters by specifying that it should trim NUL characters: str = str.Trim(Chr(0)), but I think that you should rather ask yourself why there is NUL characters in the string to start with.
Are you reading the data properly from the file? A common error is to use the Read method to read from a stream, but ignoring it's return value and thus ending up with a buffer only partly filled with data from the stream. As a byte array is filled with zeroes when you create it, the bytes not set by the Read operation would remain zero and become NUL characters when you decode the data into a string.
IsNullEmptyOrWhitespace checks if the variable itself is null, not if the string contains NULL characters. A NULL character is not a whitespace. So this check also fails.
I suggest you use a Trim(), after the test. In C# this will look like:
bool MyNullCheck(string s) {
if (s == null) return false;
s = s.Trim(new string(char.ConvertFromUtf32(0), 1));
return string.IsNullEmptyOrWhiteSpace(s);
}
Try to convert to VB (not checked)
Function MyNullCheck(s as String) as Boolean
If s Is Nothing Then
Return False
End If
s = s.Trim(New String(vbNullChar, 1))
Return String.IsNullEmptyOrWhiteSpace(s)
End Function
A null string is one that hasn't been initialised or has been set to Nothing.
An empty string is one that contains the empty string String.Empty or "".
Whitespace characters are space, tab, newline, carriage return and lots more. But not the null character.
Your string is neither empty nor Nothing. It contains 7 characters, each one is the null character - so it is not whitespace.
You could use String.Replace to remove the zero characters? Something like this
s = s.Replace(vbNullChar, "")
I bet you have run into an encoding issue. Try reading the file as UTF-16