lua substr function. param encoding - encoding

I need to use in lua an analog of the function php - mb_substr, where there is - bitwise step.
Example in php - mb_substr($value, 0, 32, '8bit');
is there anything like that lua?
edit:
bit of a bit - bitwise step

Related

perl YAML.pm dump array without quotes

I am writing a Perl script to generate a docker-compose.yml file. I am using the YAML.pl module's DumpFile method to write a complex hash to a file in YAML format.
Some of the arrays are dumping correctly, that is to say, the elements are unquoted, e.g.,
"environment" => [
'MYSQL_ROOT_PASSWORD=secret', 'MYSQL_DATABASE=db',
'MYSQL_USER=dbadmin', 'MYSQL_PASSWORD=secret2',
],
becomes
environment:
- MYSQL_ROOT_PASSWORD=secret
- MYSQL_DATABASE=db
- MYSQL_USER=dbadmin
- MYSQL_PASSWORD=secret2
However there are some arrays that contain what are supposed to be arguments with values comprised of environment variables that docker-compose will get from the environment, i.e., I CANNOT output the raw values here, I need to output the env var that docker-compose will use to get the value. These are being dumped quoted:
"args" => [
'APP_CODE_PATH=${APP_CODE_PATH_CONTAINER}',
'APP_GROUP=${APP_GROUP}',
'APP_GROUP_ID=${APP_GROUP_ID}',
'APP_USER=${APP_USER}',
'APP_USER_ID=${APP_USER_ID}',
'TARGET_PHP_VERSION=${PHP_VERSION}',
'TZ=${TIMEZONE}'
],
becomes:
args:
- 'APP_CODE_PATH=${APP_CODE_PATH_CONTAINER}'
- 'APP_GROUP=${APP_GROUP}'
- 'APP_GROUP_ID=${APP_GROUP_ID}'
- 'APP_USER=${APP_USER}'
- 'APP_USER_ID=${APP_USER_ID}'
- 'TARGET_PHP_VERSION=${PHP_VERSION}'
- 'TZ=${TIMEZONE}'
The output is not supposed to be quoted.
I've combed the YAML.pm docs, but I cannot find anything specific to this question there.
I suspect it is how I'm entering the values in the array, but I can't figure out what I'm doing wrong.
Use YAML::Syck instead. You can turn off that quoting, and it's off by default:
use v5.26;
use YAML::Syck;
my $hash = {
"args" => [
'APP_CODE_PATH=${APP_CODE_PATH_CONTAINER}',
'APP_GROUP=${APP_GROUP}',
],
};
print Dump($hash);
Now it's unquoted:
---
args:
- APP_CODE_PATH=${APP_CODE_PATH_CONTAINER}
- APP_GROUP=${APP_GROUP}
You could use YAML::PP instead. It tries to only quote things if really necessary, or it would look ambiguous.
YAML.pm is old, it was written for YAML 1.0, and it has a lot of bugs (as well as YAML::Syck). See matrix.yaml.io.
use YAML::PP qw(Dump);
print Dump($data);
Also, YAML::PP supports the official YAML 1.1 and 1.2 Schemas (regarding numbers, booleans etc.), while YAML.pm, YML::XS and YAML::Syck do not.
(Disclaimer: I'm the author of YAML::PP)
Regarding "The output is not supposed to be quoted": That should actually not matter. I don't think it would stop working just be cause the values are quoted. I couldn't imagine why that would be the case.

In Squeak Smalltalk, how can type a number which is base-250 positional numeral system?

One thing that makes me particularly like about Smalltalk is that it
has the power to do arithemtic calculations of numbers with the base
of different integers. I guess no other language can do the same.
Please see the codes below.
Transcript show: 16raf * 32; cr.
Transcript show: 7r21 - 5r32; cr.
The output is
5600
-2
I understand that if the number is hexadecimal(16-based), abcdef can
be employed. But what if the integer I want to be the base is 250. On some position, there's 60. How can I type that number in squeak ?
Short answer: you cannot type arbitrary numbers for arbitrary bases above 36 without changing the parser.
Longer answer:
You can use arbitrary base above 36, but you will run into trouble print and write numbers that would need symbols above 36.
You can check all the symbols for a base:
base := 36.
number := 0.
1 to: base - 1 do: [ :i |
number := number * base + i
].
number printStringBase: base.
the above results in the following
'123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
This is also hard-coded when printing in SmallInteger>>printOn:base:length:padded:
Note that for a number that is smaller than base, printStringBase: will just use ascii directly.
36 printStringBase: 37 '['
But even if you were to remove the hardcoded limitation and used ascii directly, you aren't helping yourself.
Sooner or later you will need ascii symbols that have different meaning in the syntax. For example following Z (ascii 90) is [ (ascii 91), which is used to start block.
So 37r2[ will be parsed into 37r2 and [ will be reserved for block (and thus will result in syntax error).
But otherwise you can use any base
2001rSpaceOdyssey -> 57685915098460127668088707185846682264

Unary "Not" or bit-flip operator in PowerShell?

I have a decimal value 163 that I need to do a unary "not" operation on. In C++ and Java, this would look like ~(163). Essentially, I want every bit to flip.
Is there a way to do this in PowerShell using a unary operator or, if needed, a subroutine? I would also need this to flip all the bits of the entire 32-bit address. In other words, 163 in binary is 0b10100011, but I would want the entire 32-bit container to be flipped including the 163 at the end (e.g. 0b11111......01011100).
As Bill_Stewart commented, the -bnot (binary not or bitwise not) operator does what you want. It works in PowerShell 2.0.
Just be aware that PowerShell's default integers are generally signed, and the result of this operation will be different depending on that.
You may need to cast to one of the unsigned types to get the result you want:
-bnot ([uint32]163)

What does this piece of perl code mean?

I'm dealing with a Perl script. Perl is quite new to me.
Does anybody have an idea what this code does?
$pval = sprintf("%0.1e", Statistics::Distributions::uprob($z));
Where :
my $z= ($all{key} - $mu) / ${sigma};
$pval is assigned a formatted string in the shape of a floating-point number in scientific notation. Presumably the function uprob from the Statistics module returns such a number.
$z is passed as argument to that function, and it seems to be a number calculated from a value in the hash %all with the key key, minus $mu, divided by $sigma. Pretty straightforward.

reading and storing numbers in perl without a loss of percision (Perl)

I have a few numbers in a file in a variety of formats: 8.3, 0.001, 9e-18. I'm looking for an easy way to read them in and store them without any loss of precision. This would be easy in AWK, but how's it done in Perl? I'm only open to using Perl. Thanks!
Also, I was wondering if there's an easy way to print them in an appropriate format. For example, 8.3 should be printed as "8.3" not "8.3e0"
If they're text strings, then reading them into Perl as strings and writing them back out as strings shouldn't result in any loss of precision. If you have to do arithmetic on them, then I suggest installing the CPAN module Math::BigFloat to ensure that you don't lose any precision to rounding.
As to your second question, Perl doesn't do any reformatting unless you ask it to:
$ perl -le 'print 8.3'
8.3
Am I missing something?
From http://perldoc.perl.org/perlnumber.html:
Perl can internally represent numbers in 3 different ways: as native
integers, as native floating point numbers, and as decimal strings.
Decimal strings may have an exponential notation part, as in
"12.34e-56" . Native here means "a format supported by the C compiler
which was used to build perl".
This means that printing the number out depends on how the number is stored internal to perl, which means, in turn, that you have to know how the number is represented on input.
By and large, Perl will just do the right thing, but you should know how what compiler was used, how it represents numbers internally, and how to print those numbers. For example:
$ perldoc -f int
int EXPR
int Returns the integer portion of EXPR. If EXPR is omitted, uses $_. You should
not use this function for rounding: one because it truncates towards 0, and two
because machine representations of floating-point numbers can sometimes produce
counterintuitive results. For example, "int(-6.725/0.025)" produces -268 rather than
the correct -269; that's because it's really more like -268.99999999999994315658
instead. Usually, the "sprintf", "printf", or the "POSIX::floor" and
"POSIX::ceil" functions will serve you better than will int().
I think that if you want to read a number in explicitly as a string, your best bet would be to use unpack() with the 'A*' format.