True.(But the eval was necessary because feeding a decimal number with a dot or a comma made a difference)
Btw, you can even make a new function out of it:
Code: Select all
function nthroot($num, $n=2) { return eval($num) ^ (1/$n); }
True.(But the eval was necessary because feeding a decimal number with a dot or a comma made a difference)
Code: Select all
function nthroot($num, $n=2) { return eval($num) ^ (1/$n); }
Code: Select all
function range($from, $to, $step=1, $seperator="|") {
if ($step == 0) {
end 1==1, "Value for step [ 0 ] is not an allowed step for range function";
}
$count = $from;
$list = "";
if ($step > 0) {
while($count <= $to) {
$list = $list . "|" . $count;
$count = $count + $step;
}
} else {
while($count >= $to) {
$list = $list . "|" . $count;
$count = $count + $step;
}
}
return formatlist($list, "e");
}
Code: Select all
text range(1, 10) //Will create a text box with 1|2|3|4|5|6|7|8|9|10 in it.
//This will loop through the range and echo 1 then 3 then 5
foreach($number, range(1, 5, 2)) {
echo $number;
}
//This will loop through the range in reverse order and echo 10, 8, 6
foreach($number, range(10, 5, -2)) {
echo $number;
}
Code: Select all
FUNCTION sendkeyescape($str) {
/* Escape special sendkeys control characters in a string,
** to enable snedkey to type out the string as-is.
** $str the string to Escape */
$str = regexreplace($str,'([\{\}\^\~\%\+])','{$1}');
return $str;
}
Code: Select all
"Config Backup ..." ::sendkeys "^k{home}fff%n";
Code: Select all
FUNCTION pad(&$stringA, &$stringB, $padding, $side = 'L', $trim = 0){
/* reciprocally pad two strings to the same length
** $stringA, $stringA the two strings
** $padding the padding char, must be one character
** if $stringA looks like a number, defaults to 0
** if $stringA looks like a string, defaults to ' '
** $side which side to pad, 'L'eft or 'R'ight
** $trim if 1, then ' ' or 0 are trimmed (NOT the padding char)
*/
$padding = substr($padding, 0, 1); //take single padding char
if ($padding == '') { if (regexmatches($stringA, '[^0-9\-\+\.]') == '') {$padding = 0; } else { $padding = ' '; } }
if ($trim == 1) {$stringA = trim($stringA, '0 '); $stringB = trim($stringB, '0 ');}
$lenA = strlen($stringA); $lenB = strlen($stringB); $diffA = 0; $diffB = 0; $diff = ($lenA - $lenB);
if ($diff < 0) {$diffA = abs($diff);} elseif ($diff > 0) {$diffB = abs($diff);}
$stringA = ($diffA == 0) ? ($stringA) : ((($side=='L')?strrepeat($padding, $diffA):'') . $stringA . (($side=='R')?strrepeat($padding, $diffA):''));
$stringB = ($diffB == 0) ? ($stringB) : ((($side=='L')?strrepeat($padding, $diffB):'') . $stringB . (($side=='R')?strrepeat($padding, $diffB):''));
}
Code: Select all
$a = 'right'; $b = 'aligned';
pad($a, $b);
text $a.<crlf>.$b;
INCLUDE "inc\pad.xyi";
/*returns
right
aligned
*/
Code: Select all
FUNCTION RemTokenPos($string, $index, $sep){
// function to remove single token
$len = gettoken($string, 'count', $sep);
if ($index <= 0) || ($index > $len) { return $string;}
$half1=gettoken( $string, $index-1, $sep, 1, 1);
$half2=gettoken($string, $index+1, $sep, 1, 2);
if ($half1=="") { $string=$half2;}
elseif ($half2=="") { $string=$half1;}
else {$string="$half1$sep$half2";}
return $string;}
FUNCTION RepTokenPos($string, $token, $index, $sep){
// function to replace token
$len = gettoken($string, 'count', $sep);
if ($index <= 0) || ($index > $len) { return $string;}
$half1=gettoken( $string, $index-1, $sep, 1, 1);
$half2=gettoken($string, $index+1, $sep, 1, 2);
if ($half1=="") { $string="$token$sep$half2";}
elseif ($half2=="") { $string="$half1$sep$token";}
else {$string="$half1$sep$token$sep$half2";}
return $string;}
FUNCTION InsTokenPos($string, $token, $index, $sep){
// function to insert token
// to insert @ beginning use 0
// else it will insert after the index
// i.e. $index=1 will insert @ 2
$len = gettoken($string, 'count', $sep);
if ($index < 0) || ($index > $len ) { return $string;}
$half1=gettoken( $string, $index, $sep, 1, 1);
$half2=gettoken($string, $index+1, $sep, 1, 2);
if ($half1=="") { $string="$token$sep$half2";}
elseif ($half2=="") { $string="$half1$sep$token";}
else {$string="$half1$sep$token$sep$half2";}
return $string;}
Code: Select all
"examples "
$a="19|20|22|22|21";
$b=5;
$nToken=30;
$sep="|";
$c=RemTokenPos($a, $b, $sep);
text $c; //19|20|22|22
$d=RepTokenPos($a, $nToken, $b, $sep);
text $d; //19|20|22|22|30
$d=InsTokenPos($a, $nToken, $b, $sep);
text $d; //19|20|22|22|21|30
Thanks again highend . It certainly got me thinking . First off realized that these functions didn't do the work I intendedhighend wrote:All the three functions allow the usage of a separator but you'll return a hardcoded format ("|"- separated).
Either allow two separators (one for the $string and one for the $token) or at least rewrite it
to return the format of the source string (and not a hardcoded "|")...
Code: Select all
$half1.$sep.$token.$sep.$half2
Code: Select all
//RemTokenPos, RepTokenPos
$len = gettoken($string, 'count', $sep);
if ($index <= 0) || ($index > $len) { return $string;}
//InsTokenPos
$len = gettoken($string, 'count', $sep);
if ($index < 0) || ($index > $len - 1) { return $string;}
Very right!SammaySarkar wrote:#1 - Concatenate variables with a '.' to avoid syntax warnings (Scripting > Syntax Checking).Code: Select all
$half1.$sep.$token.$sep.$half2
Code: Select all
"$half1$sep$token$sep$half2"
Code: Select all
"Prefix$half1$sep$token$sep$half2-Suffix"