Larry Ullman's Book Forums

# Alternate To Switch Statement

## Recommended Posts

Greetings everybody.

I have written a code for my friend web for getting the weight; sp.gravity and wall thickness for the given Pipe diameter. All is working fine, but is there any better way to shorten the code?

<!DOCTYPE html>

<html lang="en">

<meta charset="UTF-8">

<title>Pipe weight finder</title>

<script language="JavaScript">function Cal(Atext, Btext, Ctext,form){

var A = parseFloat(Atext);

var B = parseFloat(Btext);

var C = parseFloat(Ctext);

// here, the values for A= pipe dia; B= Pressure class; c=stiffness;

//the chart vlaues for K = weight/rmt; L= Specfic Gravity; M= wall thickness in mm; N= hydro test pressure in Mpa.

// the switch case statement goes in matrix manner

// for ine valve of A = three valves of B; and nine valves of C

switch (true){

case (A == 100 && B == 3 && C == 128): K =21.4; L =100; M =1.89; N=3; break ;

case (A == 100 && B == 3 && C == 256): K =21.9; L=100; M=1.92; N=3.5; break ;

case (A == 100 && B == 3 && C == 512): K =22.5; L=100; M=1.95; N=3.9; break ;

case (A == 100 && B == 6 && C == 128): K =31.5; L=100; M=2.02; N=4.1; break ;

case (A == 100 && B == 6 && C == 256): K =32.3; L=100; M=2.16; N=4.2; break ;

case (A == 100 && B == 6 && C == 512): K =32.9; L=100; M=2.69; N=4.35;break ;

case (A == 100 && B == 9 && C == 128): K =42.8; L=100; M=3.15; N=4.8; break ;

case (A == 100 && B == 9 && C == 256): K =43.6; L=100; M=3.45; N=4.9; break ;

case (A == 100 && B == 9 && C == 512): K =44.5; L=100; M=3.78; N=5.1; break ;

case (A == 150 && B == 3 && C == 128): K =65.5; L=100; M=4.86; N=6.2; break ;

case (A == 150 && B == 3 && C == 256): K =68.4; L=100; M=4.95; N=6.3; break ;

case (A == 150 && B == 3 && C == 512): K =69.2; L=100; M=5.01; N=6.4; break ;

// ... the code goes accordingly.

// the code goes up to 1000 mm (i.e A=1000; B=9; C=512)

}

form.Ans1.value = K + " Kg/Rmt";

form.Ans2.value = Math.round((K*100/3.28)/100) + " Kg/Ft" ;

form.Ans3.value = L;

form.Ans4.value = M + " mm";

form.Ans5.value = N + " Mpa";

}

</script>

<body>

<div style="text-align=center; background-color: #C0C0C0; width:225px; height:450px; border:2px double blue;">

<P><FONT SIZE="+2">Pipe weight finder</FONT><hr color="#0099FF"> </P>

<form name="ratefinder" method="post">

<P>Enter Pipe dia:

<select name="input_A" >

<option value="100">100 mm

<option value="150">150 mm

<option value="200">200 mm

<option value="250">250 mm

<!--the option value goes up to 1000 mm -->

</select></p>

<P>Enter Pressure:

<select name="input_B" >

<option value="3"> 3 Kg

<option value="6"> 6 Kg

<option value="9"> 9 Kg

</select></p>

<P>Enter Stiffness:

<select name="input_C" >

<option value="128"> 128

<option value="256"> 256

<option value="512"> 512

</select></p>

<P><input type="button" VALUE="SUBMIT" onClick="Cal(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form)">

<input type="Reset" name="reset" value ="RESET" onClick ="(this.form);" /></P>

<hr color="#0099FF">

<P>Weight: <input type=text name="Ans1" SIZE=10></P>

<P>Weight /Feet: <input type=text name="Ans2" SIZE=10></P>

<P>Sp.Gravity: <input type=text name="Ans3" SIZE=10></P>

<P>Wall Thickness: <input type=text name="Ans4" SIZE=10></P>

<P>Hydro testing: <input type=text name="Ans5" SIZE=10></P>

</form>

</div>

</body>

</html>

Even though the code works fine but:

1. Due to long JavaScript line, the page getting slower.
2. As the JS code is Matrix manner & the value are more than 15 x 3 x3 = 135 switch cases;

Kindly suggest is there any better way though PHP?

Thanking you.

##### Share on other sites

There are a variety of ways you could make this shorter. The following is just one idea.

Considering that the Atext, Btext and Ctext values form a matrix (like you stated), I'd make a matrix where each element is an array of the K, L, M and N values, in that order. For example:

```var pipe_data = [100];

pipe_data[100] = [3];

pipe_data[100][3] = [128, 256];

pipe_data[100][3][128] = [21.4, 100, 1.89, 3];

pipe_data[100][3][256] = [21.9, 100, 1.92, 3.5];

Naturally, you'd want to write a loop that populated the array for you, but I think you get the point. Once you have the array built, all you need to do is reference the three indexes using your Atext, Btext and Ctext values to get your K, L, M and N values. No looping is required with this method, which will greatly speed things up.

As a side, it might be best to put your JS after the form HTML.

##### Share on other sites

Thank you very much Mr. San,

Your suggestion is fine, but the no of lines are coming to 135 i,e

pipe_data[100][3][128] = [21.4, 100, 1.89, 3];

pipe_data[100][3][256] = [21.9, 100, 1.92, 3.5];

pipe_data[100][3][512] = [22.5, 100, 1.95, 3.9];

so this list goes up to 135 lines i.e for pipe - 15 values; pressure - 3valves; stiffness -3 values , resulting total lines would be 135 lines. whereas in the switch case, the lines are 135 - Hence there is no much difference in the code what you suggested.

Instead of putting entire JavaScript code in the HTML, I would like to put in a external js file and link to the page so that there would be better speed. Dear Mr. San, I request your attention, is there any better method with PHP?

##### Share on other sites

I think you missed my point.

First off, I suggested using multiple for loops to create the array for you. That would only require a few lines of code.

Secondly, once you create the array once, you don't need to create it anymore, which will result in a huge speed boost in your code.

The code I wrote was purely for the sake of demonstrating the structure. I certainly don't expect you to write out every matrix value like that.

Also, it's important to understand that 135 variables assignments and 135 case statement comparisons are not the same thing. The case statements take considerably more processing power, not to mention that you have to execute them each time.

Please think about my answer a bit more, and I think you will arrive at a reasonable solution.

##### Share on other sites

Sorry Mr. San, I am unable make-out your point.

Could you explain little bit more?

Also is there any alternate option for this problem through PHP?

Thanks.

##### Share on other sites

webguru, I don't know what your programming background is, but it seems like you're lacking confidence in some of the fundamentals.

While I don't mind helping you with this, I think you'd be better off reading through Larry's JavaScript book and/or PHP/MySQL book first. If you do that, then I think you'll have enough knowledge to do this on your own.

With that said, I'll explain a bit more how I would handle this. (Please keep in mind that there are many ways to handle this, including more efficient methods which are more conceptually complex.)

In your case, since you already wrote out all the case statements with all the data you need, I'd copy all those case statements into a text file, and run some search and replace operations on them until you've turned the following list of case statements into the CSV data below:

```case (A == 100 && B == 3 && C == 128): K =21.4; L =100; M =1.89; N=3; break ;
case (A == 100 && B == 3 && C == 256): K =21.9; L=100; M=1.92; N=3.5; break ;
case (A == 100 && B == 3 && C == 512): K =22.5; L=100; M=1.95; N=3.9; break ;
case (A == 100 && B == 6 && C == 128): K =31.5; L=100; M=2.02; N=4.1; break ;
case (A == 100 && B == 6 && C == 256): K =32.3; L=100; M=2.16; N=4.2; break ;
case (A == 100 && B == 6 && C == 512): K =32.9; L=100; M=2.69; N=4.35;break ;
case (A == 100 && B == 9 && C == 128): K =42.8; L=100; M=3.15; N=4.8; break ;
case (A == 100 && B == 9 && C == 256): K =43.6; L=100; M=3.45; N=4.9; break ;
case (A == 100 && B == 9 && C == 512): K =44.5; L=100; M=3.78; N=5.1; break ;
case (A == 150 && B == 3 && C == 128): K =65.5; L=100; M=4.86; N=6.2; break ;
case (A == 150 && B == 3 && C == 256): K =68.4; L=100; M=4.95; N=6.3; break ;
case (A == 150 && B == 3 && C == 512): K =69.2; L=100; M=5.01; N=6.4; break ;```

```100,3,128,21.4,100,1.89,3
100,3,256,21.9,100,1.92,3.5
100,3,512,22.5,100,1.95,3.9
100,6,128,31.5,100,2.02,4.1
100,6,256,32.3,100,2.16,4.2
100,6,512,32.9,100,2.69,4.35
100,9,128,42.8,100,3.15,4.8
100,9,256,43.6,100,3.45,4.9
100,9,512,44.5,100,3.78,5.1
150,3,128,65.5,100,4.86,6.2
150,3,256,68.4,100,4.95,6.3
150,3,512,69.2,100,5.01,6.4```

That only requires a few simple search and replace operations.

I would then save the CSV data into a text file. For this demo, we'll call the file data.txt.

Next, I'd write a PHP script like the following, which would take the CSV data and write out all the code necessary to create a JS array:

```<?php

\$data = file_get_contents('data.txt'); // Import the data in the text file.

\$arr = explode("\r\n", \$data); // Explode the data into an array on the newlines.

\$final_arr = array(); // Create a new array for all the data.

foreach (\$arr as \$v) { // This loop creates the entire array of data (in PHP). Note the type juggling.

\$row = explode(',', \$v);

\$a_val = (int) \$row[0];

\$b_val = (int) \$row[1];

\$c_val = (int) \$row[2];

if (!isset(\$final_arr[\$a_val])) {

\$final_arr[\$a_val] = array();

}

if (!isset(\$final_arr[\$a_val][\$b_val])) {

\$final_arr[\$a_val][\$b_val] = array();

}

\$final_arr[\$a_val][\$b_val][\$c_val] = array((float) \$row[3], (int) \$row[4], (float) \$row[5], (float) \$row[6]);

}

// Everything below here is for formatting the data in \$final_arr so that it efficiently creates the necessary array in JS.

\$str = 'var data = [';

/* The foreach loop and the two operations below that are repeated for all 4 levels of the
multidimensional \$final_arr array with slight variations for each level.
While the code may look complicated, it's very repetitive and mainly for formatting the output. */

foreach (\$final_arr as \$k => \$v) {

\$str .= "\$k, ";

}

\$str = rtrim(\$str, ' ,');

\$str .= "],<br>";

foreach (\$final_arr as \$k1 => \$v1) {

\$str .= "  data[\$k1] = [";

foreach (\$v1 as \$k2 => \$v2) {

\$str .= "\$k2, ";

}

\$str = rtrim(\$str, ' ,');

\$str .= "],<br>";

}

foreach (\$final_arr as \$k1 => \$v1) {

foreach (\$v1 as \$k2 => \$v2) {

\$str .= "  data[\$k1][\$k2] = [";

foreach (\$v2 as \$k3 => \$v3) {

\$str .= "\$k3, ";

}

\$str = rtrim(\$str, ' ,');

\$str .= "],<br>";

}

}

foreach (\$final_arr as \$k1 => \$v1) {

foreach (\$v1 as \$k2 => \$v2) {

foreach (\$v2 as \$k3 => \$v3) {

\$str .= "  data[\$k1][\$k2][\$k3] = [";

foreach (\$v3 as \$k4 => \$v4) {

\$str .= "\$v4, ";

}

\$str = rtrim(\$str, ' ,');

\$str .= "],<br>";

}

}

}

\$str = rtrim(\$str, ',<br>');

\$str .= ';';

echo \$str; // Output all the text in the \$str variable. This will give you the code you should copy into your JS file.

?>```

And there ya go. That will give you all the necessary JS code on a silver platter. There are slightly more efficient ways to do this, but they will create more programming headaches, so I went with this method for now.

Please study the code closely and try it out before you criticize it or ask questions about something you don't understand.

Thank you.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.