PARI:similar function like Integer.parseInt() - type-conversion

I want to convert the text hello to ascii decimal in PARI/GP. After that I will
concatenate the values.
I initialize a Vecsmall(hello), after that I run a loop to concatenate the ascii decimal values,
I want to use this concatenated value to * by certain values. The value is now in String type, In Java, there is a Integer.parseInt() to convert the string to int. I wonder if there is a similar function in PARI/GP?
v=Vecsmall("hello");'
for (i = 1, length(v), text=Str(text,v[i]););
//is there any similar function like Integer.praseInt(text) in PARI?

You can use eval
eval(text)
or else a combination of Vecsmall and fromdigits which is faster:
fromdigits(apply(n->n-49, Vec(text)))

Related

What is the fastest/easiest way to increase a number in a string variable in Powershell?

I have the following Powershell variable
$var = "AB-0045"
I would like to increase the number in the string to become "AB-0046".
I can do:
$newNumber = [int]$var.Substring($var.length -4,4) + 1
Which will give me the desired number 46, but then I have to append that 46 as a string to a new string "AB-00".
Is there a better way to do that?
Now that you have the integer, you'll have to convert back to string formatted in the way you'd like and concatenate.
I'd recommend adding to "AB-" rather than "AB-00" in case your number goes over 100.
To pad leading zeros, you can use the -f operator.
e.g. "{0:d4}" -f 45
You'll still need to get the integer first (45 in the example) from your original string.
I tested with regex class Replace() method and string class Split() method with string formatter. Split() seems faster provided your string is always in the same format. The Replace() method does not care what happens before the last 4 numbers:
# Replace Method
[regex]::Replace($var,'\d{4}$',{([int]$args[0].Value+1).ToString('0000')})
# Split method
$a,[int]$b = $var.split('-'); "{0}-{1:0000}" -f $a,++$b

Integer number too large error for hexadecimal value

For integers, I can use 'BigInt' if it throws a similar error, what about hexadecimal values? I strictly don't want to convert the hex value into an integer and then use it. Can somebody help?
val a = 0x1265465678687564534344536
<console>:1: error: integer number too large
That class BigInteger has a constructor that takes a String argument, so the simple answer is: instead of using a numeric literal, that is out of any meaningful range, create a BigInteger based on "0x1265465678687564534344536" instead!
Use the BigInteger class that takes in a string parameter and pass the radix as 16 to indicate hexadecimal string.
new BigInteger(string, 16)

MATLAB: extract numerical data from alphanumerical table and save as double

I created a list of names of data files, e.g. abc123.xml, abc456.xml, via
list = dir('folder/*.xml').
Matlab starts this out as a 10x1 struct with 5 fields, where the first one is the name. I extracted the needed data with struct2table, so I now got a 10x1 table. I only need the numerical value as 10x1 double. How can I get rid of the alphanumerical stuff and change the data type?
I tried regexp (Undefined function 'regexp' for input arguments of type 'table') and strfind (Conversion to double from table is not possible). Couldn't come up with anything else, as I'm very new to Matlab.
You can extract the name fields and place them in a cell array, use regexp to capture the first string of digits it finds in each name, then use str2double to convert those to numeric values:
strs = regexp({list.name}, '(\d+)', 'once', 'tokens');
nums = str2double([strs{:}]);

converting integer chars to numeric array in matlab

I have a char array of format a = [1.234 ; 2.345; 3.456] and I need to convert this to a numeric array in MATLAB. I have tried str2num(a) but it only seems to work on integers, as it is returning an empty vector. Here is what the data actually looks like:
Any suggestions on how to tackle this problem are appreciated!
If your character array is either of the following formats:
a = '[1.234; 2.345; 3.456]'; % 1-by-N with brackets, spaces, or semicolons
a = ['1.234'; '2.345'; '3.456']; % M-by-N
Then str2num should work as you want:
vec = str2num(a)
vec =
1.234000000000000
2.345000000000000
3.456000000000000
If it's not working then that probably means that your character array val has rows with invalid formats or characters that don't properly convert. Since the array has 3100 rows, you probably don't want to search through it by hand. One easy way to highlight where invalid rows might be is to identify where there are characters other than numbers, periods, or white space. Here's how you can get a list of rows that may warrant further inspection:
suspiciousRows = find(~all(ismember(val, '0123456789. '), 2));
The function str2double would works for your case. Please refer to this link for detailed usage.
str2num would work as it is but as str2num uses eval,so a better alternative is str2double. But it is not directly applicable on a char array like yours. You can convert that array into a cell using cellstr and then apply str2double.
req = str2double(cellstr(val))
Another approach if you have MATLAB R2016b or a later version is to convert that char array into a string array and then apply str2double.
req = str2double(string(val))

how to combine two arrays in php?

I am having one array. For example :
array('a'=>'abc','b'=>'pqr','c'=>'xyz');
fron this i have encoded the key 'c'
now i am getting encoded value for this key.
next i need to put this encoded value in place of the original value of encoded key...
example i wnt output like this :
array('a'=>'abc','b'=>'pqr','c'=>H162);
please anybody help me.
Just assign a value to element 'c' of the array. e.g.
$arr = array('a'=>'abc','b'=>'pqr','c'=>'xyz');
print_r($arr);
$arr['c'] = 'H162';
print_r($arr);
array_merge — Merge one or more arrays
if you just want to change the value of 'c' and you know the key, you can simply call something like this
$your_array['c'] = NEW_VALUE
But this hasn't something to do with combining arrays. If you wan't to combine 2 arrays have a look at http://php.net/manual/de/function.array-combine.php
I believe this is what you are trying to achieve:
$array1 = array('a'=>'abc','b'=>'pqr','c'=>'xyz');
$array2 = array('xyz'=>'test');
foreach($array1 as $key=>$element){
if(array_key_exists($element, $array2)){
$array1[$key] = $array2[$element];
}
}
Very broad question. I put an example with numbers, several encoding/decoding methods exist for strings.
First, define your encode/decode functions. (Note: In this example i work with positive numbers. You could write you own encoding methods for strings). When you access your items, you must always know whether the value is encoded or not, so we always represent encoded numbers as negative numbers, and we assume negative numbers are encoded numbers. (For strings you can precede normal strings with "0" and encoded strings with "1" for example. Other methods exist.)
//Very simple functions, should be complex functions.
function encode($x) { return - $x * 2; }
function decode($x) { return - $x / 2; }
Now imagine an array:
$arr = array('a'=>123,'b'=>456,'c'=>789);
To encode the 'c':
$arr['c'] = encode($arr['c']);
...or encoding all items in your array:
foreach($arr as $key=>$val)
$arr[$key] = encode($arr[$key]);
For accessing the array members:
function getArrayMember($key)
{
if ($arr[$key] < 0) //This is an encoded number...
return decode($arr[$key]); //...decode it.
else //Normal numbers...
return $arr[$key]; //...return as is.
}
This was for numbers. You could implement or find suitable encoding/decoding methods for strings.