php switch case statement to handle ranges - php

I'm parsing some text and calculating the weight based on some rules. All the characters have the same weight. This would make the switch statement really long can I use ranges in the case statement.
I saw one of the answers advocating associative arrays.
$weights = array(
[a-z][A-Z] => 10,
[0-9] => 100,
['+','-','/','*'] => 250
);
//there are more rules which have been left out for the sake of clarity and brevity
$total_weight = 0;
foreach ($text as $character)
{
$total_weight += $weight[$character];
}
echo $weight;
What is the best way to achieve something like this?
Is there something similar to the bash case statement in php?
Surely writing down each individual character in either the associative array or the switch statement can't be the most elegant solution or is it the only alternative?

Well, you can have ranges in switch statement like:
//just an example, though
$t = "2000";
switch (true) {
case ($t < "1000"):
alert("t is less than 1000");
break
case ($t < "1801"):
alert("t is less than 1801");
break
default:
alert("t is greater than 1800")
}
//OR
switch(true) {
case in_array($t, range(0,20)): //the range from range of 0-20
echo "1";
break;
case in_array($t, range(21,40)): //range of 21-40
echo "2";
break;
}

$str = 'This is a test 123 + 3';
$patterns = array (
'/[a-zA-Z]/' => 10,
'/[0-9]/' => 100,
'/[\+\-\/\*]/' => 250
);
$weight_total = 0;
foreach ($patterns as $pattern => $weight)
{
$weight_total += $weight * preg_match_all ($pattern, $str, $match);;
}
echo $weight_total;
*UPDATE: with default value *
foreach ($patterns as $pattern => $weight)
{
$match_found = preg_match_all ($pattern, $str, $match);
if ($match_found)
{
$weight_total += $weight * $match_found;
}
else
{
$weight_total += 5; // weight by default
}
}

You can specify the character range using regular expression. This saves from writing a really long switch case list. For example,
function find_weight($ch, $arr) {
foreach ($arr as $pat => $weight) {
if (preg_match($pat, $ch)) {
return $weight;
}
}
return 0;
}
$weights = array(
'/[a-zA-Z]/' => 10,
'/[0-9]/' => 100,
'/[+\\-\\/*]/' => 250
);
//there are more rules which have been left out for the sake of clarity and brevity
$total_weight = 0;
$text = 'a1-';
foreach (str_split($text) as $character)
{
$total_weight += find_weight($character, $weights);
}
echo $total_weight; //360

Much different ways to do this.
$var = 0;
$range_const = range(10,20);
switch ($var) {
case 1: $do = 5; break; # 1
case 2: $do = 10; break; # 2
case 3:
case 4:
case 5: $do = 15; break; # 3, 4, 5
default:
if ($var > 5 && $var < 10) { # High performance (6..9)
$do = 20;
} else if (in_array($var, $range_const, true)) { # Looks clear (10..20)
$do = 25;
} else { # NOT in range 1..20
$do = -1;
}
}
print($do);
There no direct range X..Y compares because $var checks to true in each step, but this allows do some nice cheating like this...
$in = create_function('$a,$l,$h', 'return $a>=$l && $a<=$h;');
$var = 4;
switch (true) {
case ($var === 1): echo 1; break;
case ($var === 2): echo 2; break;
case $in($var, 3, 5): echo "3..5"; break;
case $in($var, 6, 10): echo "6..10"; break;
default: echo "else";
}

If you have a more complex conditions, you can wrap them inside a function. Here's an oversimplified example:
$chartID = 20;
$somethingElse = true;
switch (switchRanges($chartID, $somethingElse)) {
case "do this":
echo "This is done";
break;
case "do that":
echo "that is done";
break;
default:
echo "do something different";
}
function switchRanges($chartID, $somethingElse = false)
{
if (in_array($chartID, [20, 30]) && $somethingElse === true) {
return "do this";
}
if (in_array($chartID, [20, 50]) && $somethingElse === false) {
return "do that";
}
}

I think I would do it in a simple way.
switch($t = 100){
case ($t > 99 && $t < 101):
doSomething();
break;
}

Related

How get a (fixed) value based on number range

I have a number, e.g. $humidity
I need to check value of this number and get a fixed value if number is in a predetermined range.
E.g.
if ($humidity<30) {
return 'dry';
}
if ($humidity>=30 && $humidity <=40) {
return 'wet'
}
if ($humidty>40 && $humidity<70) {
return 'confortable'
}
And so on.
Is there another possibility to don't use 4/5 different if ?
As long as you process the values in order, you don't need both the upper and lower values of each range. Then you can utilize short-circuiting and just put everything in a loop:
function nameOf($humidity)
{
$list = [
30 => 'dry',
40 => 'wet',
70 => 'comfortable',
];
foreach ($list as $value => $name) {
if ($humidity < $value) {
return $name;
}
}
return 'default';
}
I think switch is great for this usage:
$result = null;
switch(true) {
case $humidity < 30:
$result = 'dry';
break;
case $humidity >= 30 && $humidity < 40:
$result = 'wet';
break;
case $humidty > 40 && $humidity < 70:
$result = 'comfortable';
break;
}
return $result;
you can create an range array for your temps and then array_walk that to find the right range.
$h=40;
$harr = [ 0 => 'dry', 1 => 'wet', 2 => 'confy'];
$range = array_flip(range(0, 100, 30));
array_walk($range, 'findHumidity', $range);
var_dump($harr[$result]);
function findHumidity($value, $key, $r){
global $h; //if you using a class and properties you can ignore these two lines
global $result;
$normalRange = array_flip($r);
if($h> $key && $normalRange[$value+1]!=null && $h<= $normalRange[$value+1]){
$result = $value;
}
}
Working example: https://3v4l.org/fWLDu (a simplified version: https://3v4l.org/ROjWk)
or you can define the range array manually like here: https://3v4l.org/B0oTU

Using unique random numbers for a switch statement

I need a function or an array that gives me random numbers from 1 - 47, so I can use it for my code below.
public function output($question_id, $solution) {
$yes = ($GLOBALS['TSFE']->sys_language_uid == 0) ? 'Ja' : 'Oui';
$no = ($GLOBALS['TSFE']->sys_language_uid == 0) ? 'Nein' : 'No';
switch ($question_id) {
case 1:
$arg = array(
'main_content' => $this->build_html(1, '01_0429_04_14_Psori_Arthro.jpg', $yes, $no, $solution)
);
break;
case 2:
$arg = array(
'main_content' => $this->build_html(2, '02_0342_05_14_Psori_Arthropathie.jpg', $yes, $no, $solution),
);
break;
case 3:
$arg = array(
'main_content' => $this->build_html(3, '03_0255_05_14_Psori_Arthropathie.jpg', $yes, $no, $solution),
);
break;
}
}
Example
This
case 1:
$arg = array(
'main_content' => $this->build_html(1, '01_0429_04_14_Psori_Arthro.jpg', $yes, $no, $solution)
);
should look somehow like this:
case $random_id:
$arg = array(
'main_content' => $this->build_html($random_id, '01_0429_04_14_Psori_Arthro.jpg', $yes, $no, $solution)
);
So that means, each case and each first parameter of the function build_html should get a unique random id.
Of course I could use rand() but then it is quite possible that I get duplicated values.
Any help would be appreciated!
Create a class like follows:
class SamplerWithoutReplacement {
private $pool;
public __construct($min,$max) {
$this->pool = range($min,$max);
}
public function next() {
if (!empty($this->pool)) {
$nIndex = array_rand($this->pool);
$value = $this->pool[$nIndex];
unset($this->pool[$nIndex]);
return $value;
}
return null; //Or throw exception, depends on your handling preference
}
}
Use it as:
$sampler = new SamplerWithoutReplacement(1,47);
//do things
$value = $sampler->next();
The $sampler will draw random samples between 1-47 without replacing them in the pool and therefore they'll be unique.
Not tested but I think it should work:
$numbers = [];
function genenerate_nr_not_in_list($list, $min = 1, $max = 47) {
$number = rand($min, $max);
while (false !== array_search($number, $list)) {
$number = rand($min, $max);
}
return $number;
}
for ($i = 0; $i < 47; $i++) {
$numbers[] = genenerate_nr_not_in_list($numbers);
}
With this solution you can generate numbers in each range ($min and $max parameters). But it should be at least as big as the number of values you need ($max).

String to Bool Conversion ... works in 4 bit but not 8 bit

Trying to put together a php program that reads a 'menu' integer variable (passed through the URL) that converts it to a binary string ... then reads each Boolean in the string and adds it to an array.
Works fine for 4-bit (albeit perhaps not very efficient) .... but won't work for 8-bit?
<?php
$menubin = decbin($_GET['menu']);
$menulen = strlen($menubin)-1;
for($x=$menulen; $x > -1; $x--)
{
$array = $menulen-$x;
$menubool[$array] = substr($menubin,$x,1);
}
if($menubool[0] == '1') { ... }
if($menubool[1] == '1') { ... }
if($menubool[2] == '1') { ... }
if($menubool[3] == '1') { ... }
if($menubool[4] == '1') { ... }
if($menubool[5] == '1') { ... }
if($menubool[6] == '1') { ... }
if($menubool[7] == '1') { ... }
?>
Assuming that your $menubin string looks something like 0100. This can be achieved with bitwise operands and a switch statement. This approach will give you some flexibility and allow you to select menu items arbitrarily.
<?php
//string of selections
$string = "1011";
// result of bitshift on string
$bits = 0;
for($i = strlen($string); $i >= 0; $i -= 1){
$bits += (int)$string[$i] << $i;
}
// arbitrarily make selections and cases as you add menu items
switch($bits) {
case 1:
echo "menu item 1";
break;
case 2:
echo "menu item 2";
break;
case 4:
echo "menu item 3";
break;
case 8:
echo "menu item 4";
break;
case 13:
echo "menu item 1, 3 and 4";
break;
}
?>
You can do something like this:
$int = 37; // for example
$bits = array();
$str = str_pad(decbin($int),8,"0",STR_PAD_LEFT);
for ($i=0; $i<=7; $i++){
$bits[7-$i] = substr($str,$i,1);
}
var_dump($bits);
Then just access the bits of the array as you require for your menu options.
Your initial problem was that decbin doesn't return the correct string length, hence the padding function above.
In your case, 64 would return 1000000 and not 01000000

variable dynamic span overlap

$lines = file("res_example.txt");
$resArr = array();
foreach ($lines as $line_num => $line) {
$columns = explode("\t", $line);
$raws = $columns['1'];
$hits = $columns['2'];
$names = $columns['0'];
$models = $columns['3'];
$colors = $columns['4'];
$allModels[$models] = 1;
$resArr[] = array(
name => $names,
score => $raws,
hit => $hits,
model => $models,
color => $colors
);
}
$seqArr = array('A', 'T', 'C', 'G');
$randseq = array();
for ($i = 0; $i < 1000; $i++) {
$randseq[] = $seqArr[array_rand($seqArr)];
}
$res = "";
echo "<div id=\"coltext\" style=\"font-family:monospace;\">";
foreach ($allModels as $modName => $value) {
echo "<input ModelName=$modName type=\"checkbox\"
checked==\"TRUE\" onclick=\"toggle.apply(this)\" />$modName";
}
echo "<hr />";
$score = rawtransform($raw);
foreach ($randseq as $index => $nuc) {
$index = $index + 1;
foreach ($resArr as $hitArr) {
$hit = $hitArr['hit'];
$raw = $hitArr['score'];
$model = $hitArr['model'];
$colval = $hitArr['color'];
$score = rawtransform($raw);
$color = getcolor($score, $colval);
if (($hit+3) == $index) {
echo "</span>";
}
if ($hit == $index) {
echo "<span class=$model Title=\"position:$index,score:$raw\"
style=\"background:$color;\" color=\"$color\">";
//problem when theres overlap !?
}
}
echo $nuc;
if (($index%50)==0){
echo"<br />";
}
}
echo "</div>";
function rawtransform($raw) {
return (int)($raw/50)-9;
}
function getcolor($score,$ArrayModelval)
{
switch ($score){
// working. test each color.
case 1: /*500-550(Raw Score)*/
$col=$ArrayModelval;
return"hsl( $col,100%,90%)";
break;
case 2: //550-600
$col=$ArrayModelval;
return "hsl( $col,100%,85%)";
break;
case 3: //600-650
$col=$ArrayModelval;
return "hsl( $col,100%,85%)";
break;
case 4: //650-700
$col=$ArrayModelval;
return"hsl( $col,100%,80%)";
break;
case 5: //700-750
$col=$ArrayModelval;
return"hsl( $col,100%,70%)";
break;
case 6: //750-800
$col=$ArrayModelval;
return "hsl( $col,100%,60%)";
break;
case 7: //800-850
$col=$ArrayModelval;
return "hsl( $col,100%,50%)";
break;
case 8: //850-900;
$col=$ArrayModelval;
return "hsl( $col,100%,50%)";
break;
case 9: //900-950
$col=$ArrayModelval;
return "hsl( $col,100%,40%)";
break;
case 10: //950-1000
$col=$ArrayModelval;
return "hsl($col,100%,40%)";
break;
}
}
For the most part does what I want: I want to color parts of the random seqeunce where there is a $hit - defined on external file. My only problem is when there is any overlap, i.e. if two hits are within 3 bases of each other the span is elongated and colored as if its one span.
The external file has a position to start a span which have variable colors depending on a score given in the external file. Basically if I have 3 results, 2 of which have almost the same hit (+-1) and the other a different hit, I would only see two sections colored, can anyone see what my problem is? Sorry I know I probably worded this horribly but its hard to explain. Thanks.
>chr1:2198584545754_genome_1000+ 500 1000 Model1 0
>chr2:2198581212154_genome_1000+ 510 992 Model2 180
>chr3:2115151215754_genome_1000+ 520 990 Model3 330
>chr4:2198584545754_genome_1000+ 530 980 Model3 330
>chr5:1218455145754_genome_1000+ 540 970 Model2 180
>chr6:1231354645454_genome_1000+ 550 960 Model1 0
>chr7:1231213211134_genome_1000+ 600 950 Model3 330
>chr7:1231213211134_genome_1000+ 650 940 Model3 330
javascript:
function toggle() {
var div= document.getElementById('coltext');
var modName=this.getAttribute('ModelName');
var spans=div.getElementsByTagName('span');
var spans_l=spans.length;
while (spans_l--){
span=spans[spans_l];
if(span.getAttribute('class')==modName && this.checked==true){
var color= span.getAttribute('color');
span.style.background=color;
}
if(span.getAttribute('class')==modName && this.checked==false){
span.style.background="white";
}
}
}
Try this on for size. It works by using a FIFO stack $currentHits to handle the hit boundaries. I have also added a few helper functions to deal with color generation - if you alter your getcolor() function to return an array instead of a CSS string one of them could be dropped, making it more efficient.
I have been unable to test this because I don't have the source code for your getcolor() or rawtransform() functions - if you add these to the question I am certain further improvements can be made and I can test the code properly. Also, it would be good to see what your CSS Model1, Model2 etc classes look like.
Edit: now includes getcolor()/rawtransform() functions and (at least partially) tested
<?php
function rawtransform ($raw) {
return (int) ($raw / 50) - 9;
}
function getcolor ($score, $h) {
switch ($score) {
// working. test each color.
case 1: /*500-550(Raw Score)*/
$l = 90;
break;
case 2: case 3: //550-650
$l = 85;
break;
case 4: //650-700
$l = 80;
break;
case 5: //700-750
$l = 70;
break;
case 6: //750-800
$l = 60;
break;
case 7: case 8: //800-900;
$l = 50;
break;
case 9: case 10: default: //900-1000 / out of range
$l = 40;
break;
}
return array(
'h' => $h,
's' => 100,
'l' => $l
);
}
function hsl_average_color () {
// Takes an unlimited number of arguments, calculates the average HSL value and returns a CSS string
$args = func_get_args();
$h = $s = $l = array();
foreach ($args as $arg) {
$h[] = $arg['h'];
$s[] = $arg['s'];
$l[] = $arg['l'];
}
return sprintf('hsl(%d, %d%%, %d%%)', (int) round(array_sum($h) / count($h)), (int) round(array_sum($s) / count($s)), round(array_sum($l) / count($l)));
}
$fileName = 'res_example.txt';
// Open the file
if (!$fp = fopen($fileName, 'r')) {
// Handle file read errors here
die("Unable to open file $fileName");
}
// Loop the file data and build an associative array
$resArr = array();
while (($line = fgetcsv($fp, 0, "\t")) !== FALSE) {
// You didn't declare $allModels as an empty array before the loop
// Should you have?
$allModels[$line[3]] = 1;
// Note that I have dropped the hit key and instead keyed the outer
// array by this value. I have added an end key to track the end of
// a hit
$resArr[$line[2]] = array(
'name' => $line[0],
'score' => $line[1],
'end' => $line[2] + 4,
'model' => $line[3],
'color' => getcolor(rawtransform($line[1]), $line[4])
);
}
// Close the file
fclose($fp);
// Generate a random sequence
$seqArr = array('A', 'T', 'C', 'G');
$randseq = array();
for ($i = 0; $i < 1000; $i++) {
$randseq[] = $seqArr[array_rand($seqArr)];
}
// $res appears to do nothing in you code
// $res = "";
// Open the <div>
echo '<div id="coltext" style="font-family:monospace;background-color:#000000;color:#FFFFFF;">'."\n";
// Iterate over $allModels and echo checkboxes
foreach ($allModels as $modName => $value) {
// ModelName is a non-standard HTML attribute, are you sure you meant to do this?
echo '<input ModelName="'.$modName.'" type="checkbox" checked="checked" onclick="toggle.apply(this);" />'.$modName."\n";
}
echo "<hr />\n";
// This line does nothing useful here
// $score = rawtransform($raw);
// An array to track the current hits
$currentHits = array();
foreach ($randseq as $index => $nuc) {
// Increment $index
$index++;
// Track whether we are in a hit/reached a boundary
$boundary = FALSE;
$inHit = (bool) count($currentHits);
// Check whether we are at the end of the lowest hit in the stack
if ($inHit && $index == $currentHits[0]['end']) {
$boundary = TRUE;
array_shift($currentHits);
}
// Check whether we are at the start of a new hit
if (isset($resArr[$index])) {
$boundary = TRUE;
$currentHits[] = $resArr[$index];
}
// If we reached a boundary
if ($boundary) {
// Close a hit
if ($inHit) {
echo "</span>";
}
// Open a hit
if (count($currentHits)) {
// Get the current color value
$colors = array();
foreach ($currentHits as $hit) $colors[] = $hit['color'];
$color = call_user_func_array('hsl_average_color', $colors);
// Start a new span
echo '<span class="'.$currentHits[0]['model'].'" title="position:'.$index.',score:'.$currentHits[0]['score'].'" style="color: '.$color.';">';
}
}
// Print the character
echo $nuc;
// Split into 50 character chunks
if (!($index % 50)){
echo"<br />\n";
}
}
// Close the last span if one is still open
if (count($currentHits)) {
echo "</span>";
}
// Close the <div>
echo "</div>\n";

Operators (operands?) as Parameters

I am trying to avoid duplicating my code by checking the variable if it is a certain operator.
Basically..
$op = $_POST['operator'];
$x = 5;
$y = 2;
$result = $x /* $op instead of '+'/'-'/'*'/'/'/'%' */ $y;
Is this possible or will I have to send the operator as a String and duplicate the code per operator type?
It's a lot safer to do something like this:
$x = 5;
$y = 2;
switch($_POST['operator']){
case '+':
$result = $x + $y;
break;
case '-':
$result = $x - $y;
break;
case '*':
$result = $x*$y;
break;
case '/':
$result = $x/$y;
break;
case '%':
$result = $x % $y;
break;
default:
$result = 'Operator not supported';
}
Something along those lines.
Ahem. You can eval.
$result = eval("$x $op $y");
But this is DANGEROUS and you should sanitize your variables with great care. There is a saying that goes something like "If your problem requires use of eval, then the problem is wrong." Something like that. It's almost certainly preferable to do something like this:
function apply_op($x, $y, $op) {
switch ($op) {
case '+': return $x + $y;
...
}
}
you can make this:
$operators = array("+", "-","*","%","/");
$op = $_POST["operator"];
if(in_array($op, $operators)) {
echo eval("$x $op $y");
} else {
echo "Operator not supported";
}

Categories