Setting up and writing an array to file


BH2114
 Share

Recommended Posts

In the following code, I have a long list of string items that get written to a file. I suspect that using an array would be a more efficient way of doing this. that being the case, could you provide an example of how this is done?

	while(1)
private st = systime()
while(systime() - st < logInterval*60)
   delay(.2)
   if (logflag==0)
      break
   endif
endwhile
 if (logflag==0)
      break
   endif
   
private string k =format("%4.2f",TorqueOutput[0])
private string l =SpeedOutput[0]
private string m =format("%3.1f",Temp1[0])
private string n =format("%3.1f",Temp2[0])
private string o =format("%3.1f",Temp3[0])
private string p =format("%3.1f",Temp4[0])
private string q =format("%3.1f",Temp5[0])
private string r =format("%3.1f",Temp6[0])
private string s =format("%3.1f",Temp7[0])
private string t =format("%3.1f",Temp8[0])
private string u =format("%3.1f",Temp9[0])
private string v =format("%3.1f",Temp21[0])
private string w =format("%4.2f",Horsepower[0])
private string x =formatdatetime("%m/%d/%Y %H:%M:%S",systime())
private string crlf =  chr(13) + chr(10)
private string a = '<td class="tg-yw4l">'
private string deg = chr(176)+' C</td>'
FileHandle = File.Open(NewFileName,0,1,1,1)
     
      private string out=    '<tr>'
      out +=    crlf 
      out +=    a
      out +=    x 
      out +=   '</td>'
      out +=    crlf
      out +=    a
      out +=    k 
      out +=   ' Ft. Lbs</td>'
      out +=    crlf
      out +=    a
      out +=    l  
      out +=   ' RPM</td>'
      out +=    crlf
      out +=    a
      out +=    m  
      out +=    deg
      out +=    crlf
      out +=    a
      out +=    n 
      out +=    deg
      out +=    crlf
      out +=    a
      out +=    o 
      out +=    deg
      out +=    crlf
      out +=    a
      out +=    p  
      out +=    deg
      out +=    crlf
      out +=    a
      out +=    q  
      out +=    deg
      out +=    crlf
      out +=    a
      out +=    r 
      out +=    deg
      out +=    crlf
      out +=    a
      out +=    s 
      out +=    deg
      out +=    crlf
      out +=    a
      out +=    t
      out +=    deg
      out +=    crlf
      out +=    a
      out +=    u  
      out +=    deg
      out +=    crlf
      out +=    a
      out +=    v 
      out +=    deg
      out +=    crlf
      out +=    a
      out +=    w 
      out +=   ' HP</td>'
      out +=    crlf
      out +=   '</tr>'
      out +=    crlf
File.Write(FileHandle,out)
File.Close(FileHandle)    
wait(.2)
endwhile
	FileHandle = File.Open(NewFileName,0,1,1,1)
      private string out=    '</table>'
      out +=     crlf
      out +=     '</html>
	

 

Link to comment
Share on other sites

actually, upon more consideration, I think that this makes more sense:

	while(1)
private st = systime()
while(systime() - st < logInterval*60)
   delay(.2)
   if (logflag==0)
      break
   endif
endwhile
 if (logflag==0)
      break
   endif
   
private string k =format("%4.2f",TorqueOutput[0])
private string l =SpeedOutput[0]
private string m =format("%3.1f",Temp1[0])
private string n =format("%3.1f",Temp2[0])
private string o =format("%3.1f",Temp3[0])
private string p =format("%3.1f",Temp4[0])
private string q =format("%3.1f",Temp5[0])
private string r =format("%3.1f",Temp6[0])
private string s =format("%3.1f",Temp7[0])
private string t =format("%3.1f",Temp8[0])
private string u =format("%3.1f",Temp9[0])
private string v =format("%3.1f",Temp21[0])
private string w =format("%4.2f",Horsepower[0])
private string x =formatdatetime("%m/%d/%Y %H:%M:%S",systime())
private string crlf =  chr(13) + chr(10)
private string a = '<td class="tg-yw4l">'
private string deg = chr(176)+' C</td>'
FileHandle = File.Open(NewFileName,0,1,1,1)
     
      private string out=    '<tr>'
      out +=    crlf + a + x + '</td>' 
      out +=    crlf + a + k + ' Ft. Lbs</td>'
      out +=    crlf + a + l + ' RPM</td>'
      out +=    crlf + a + m + deg  
      out +=    crlf + a + o + deg      
      out +=    crlf + a + p + deg
      out +=    crlf + a + q + deg
      out +=    crlf + a + r + deg
      out +=    crlf + a + s + deg 
      out +=    crlf + a + t + deg
      out +=    crlf + a + u + deg
      out +=    crlf + a + v + deg
      out +=    crlf + a + w + deg
      out +=    crlf + a + w + ' HP</td>'
      out +=    crlf + '</tr>'
	File.Write(FileHandle,out)
File.Close(FileHandle)    
wait(.2)
endwhile
	FileHandle = File.Open(NewFileName,0,1,1,1)
      private string out=    '</table>'
      out +=     crlf + '</html>
	

 

Link to comment
Share on other sites

It does, but I'd use longer variable names.  I general reserve single letter variable names for counters in things like for() loops.

I also probably wouldn't do the separate assignment for each item.  I'd definitely shorten crlf, your <td>, and deg, though I'd rename "a" to at least "td" so its clear, then I'd leave the rest, so instead of:

out += crlf + a + m + deg

I'd put:

out += crlf + td + format("%3.1f",Temp1[0]) + deg

Its much clearer and you don't have to constantly scroll up your code to figure out what m, o, and p are.  There just isn't a good reason to assign them to a variable even if the variable name is more than 1 letter.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

 Share