<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'More efficient way of checking parameters' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'More efficient way of checking parameters' posted on the 'Pascal' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Tue, 18 Jun 2013 17:09:20 -0700</pubDate>
    <lastBuildDate>Tue, 18 Jun 2013 17:09:20 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>More efficient way of checking parameters</title>
      <link>http://www.programmersheaven.com/mb/pasprog/373258/373258/more-efficient-way-of-checking-parameters/</link>
      <description>Hello,&lt;br /&gt;
&lt;br /&gt;
I'm very new to Pascal, been learning about it for around 4hrs now. Below is the start of a program that is in the works. It will be a command line application taking three parameters: start ip (sIp) end ip (eIp) and a DNS server address.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;

program dnsaudit;

uses
	sockets, inetaux;


var
i: integer;
sIp, eIp: AnsiString;
gtOpt: Pchar;

begin (* Main *)

	for i := 0 to ParamCount do
		begin
			if ParamStr(i) = '-s' then
				begin
					sIp := ParamStr(i +1);
					writeln(sIp);
				end;
			if ParamStr(i) = '-e' then
				begin
					eIp := ParamStr(i +1);
					writeln(eIp);
				end
			 else 
			 	begin
			 		if ParamStr(i) &amp;lt;&amp;gt; '-s' then
					begin
						if ParamStr(i) &amp;lt;&amp;gt; '-e' then
						begin
							if ParamStr(i) &amp;lt;&amp;gt; eIp then
							begin	
								if ParamStr(i) &amp;lt;&amp;gt; sIp then
								begin
									writeln('Invalid parameter or input specified, exiting...');
									exit();
								end
								
							end
						end
						
					end
				end;
		end;

//writeln(ParamCount);


end. (* Main *)&lt;/pre&gt; &lt;br /&gt;
&lt;br /&gt;
I would like the application to check each parameter and either assign a value from the parameter or exit if invalid data is passed to the application. As you can see the nested if statements are not the most efficient way of checking for invalid input. Could someone please demonstrate a more efficient way of performing this task?&lt;br /&gt;
&lt;br /&gt;
I've tried using the case statement to assign the parameters as a variable and or to logically check data passed to the application. Of course neither of these attempts worked because of the data-types used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/pasprog/373258/373258/more-efficient-way-of-checking-parameters/</guid>
      <pubDate>Wed, 09 Jul 2008 01:40:14 -0700</pubDate>
      <category>Pascal</category>
    </item>
    <item>
      <title>Re: More efficient way of checking parameters</title>
      <link>http://www.programmersheaven.com/mb/pasprog/373258/373270/re-more-efficient-way-of-checking-parameters/#373270</link>
      <description>: Hello,&lt;br /&gt;
: &lt;br /&gt;
: I'm very new to Pascal, been learning about it for around 4hrs now. &lt;br /&gt;
: Below is the start of a program that is in the works. It will be a &lt;br /&gt;
: command line application taking three parameters: start ip (sIp) end &lt;br /&gt;
: ip (eIp) and a DNS server address.&lt;br /&gt;
: &lt;br /&gt;
: &lt;pre class="sourcecode"&gt;: 
: 
: program dnsaudit;
: 
: uses
: 	sockets, inetaux;
: 
: 
: var
: i: integer;
: sIp, eIp: AnsiString;
: gtOpt: Pchar;
: 
: begin (* Main *)
: 
: 	for i := 0 to ParamCount do
: 		begin
: 			if ParamStr(i) = '-s' then
: 				begin
: 					sIp := ParamStr(i +1);
: 					writeln(sIp);
: 				end;
: 			if ParamStr(i) = '-e' then
: 				begin
: 					eIp := ParamStr(i +1);
: 					writeln(eIp);
: 				end
: 			 else 
: 			 	begin
: 			 		if ParamStr(i) &amp;lt;&amp;gt; '-s' then
: 					begin
: 						if ParamStr(i) &amp;lt;&amp;gt; '-e' then
: 						begin
: 							if ParamStr(i) &amp;lt;&amp;gt; eIp then
: 							begin	
: 								if ParamStr(i) &amp;lt;&amp;gt; sIp then
: 								begin
: 									writeln('Invalid parameter or input specified, exiting...');
: 									exit();
: 								end
: 								
: 							end
: 						end
: 						
: 					end
: 				end;
: 		end;
: 
: //writeln(ParamCount);
: 
: 
: end. (* Main *)&lt;/pre&gt;:  &lt;br /&gt;
: &lt;br /&gt;
: I would like the application to check each parameter and either &lt;br /&gt;
: assign a value from the parameter or exit if invalid data is passed &lt;br /&gt;
: to the application. As you can see the nested if statements are not &lt;br /&gt;
: the most efficient way of checking for invalid input. Could someone &lt;br /&gt;
: please demonstrate a more efficient way of performing this task?&lt;br /&gt;
: &lt;br /&gt;
: I've tried using the case statement to assign the parameters as a &lt;br /&gt;
: variable and or to logically check data passed to the application. &lt;br /&gt;
: Of course neither of these attempts worked because of the data-types &lt;br /&gt;
: used.&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
You could use an if-then-else-if-then-else-construction:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
  if ParamStr(i) = '-s' then
  begin
  end else if ParamStr(i) = '-e' then
  begin
  end else 
  begin
    // ParamStr(i) is neither a -e nor a -s
  end;
&lt;/pre&gt;&lt;br /&gt;
It is also better to use a while-loop instead of a for-loop, because you can control the counter:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
  &lt;strong&gt;i := 1;&lt;/strong&gt;
  while i &amp;lt;= ParamCount do
  begin
    if (ParamStr(i) = '-s') and (i &amp;lt;ParamCount)  then
    begin
      i++;
      data = ParamStr(i);
    end else if ParamStr(i) = '-e' then
    begin
      i++;
      data = ParamStr(i);
    end else 
    begin
      // Error in parameters
    end;
    i++;
  end;
&lt;/pre&gt;&lt;br /&gt;
Also note how i begins at 1. ParamStr 0 = executable name.</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/pasprog/373258/373270/re-more-efficient-way-of-checking-parameters/#373270</guid>
      <pubDate>Wed, 09 Jul 2008 06:46:05 -0700</pubDate>
      <category>Pascal</category>
    </item>
    <item>
      <title>Re: More efficient way of checking parameters</title>
      <link>http://www.programmersheaven.com/mb/pasprog/373258/373297/re-more-efficient-way-of-checking-parameters/#373297</link>
      <description>For your consideration:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
program dnsaudit ;

    Function sIp_Valid (Str : String) : Boolean ;
    begin
        {
            a stub
        }
    end ;

    Function eIp_Valid (Str : String) : Boolean ;
    begin
        {
            a stub
        }
    end ;

    Function DNS_Valid (Str : String) : Boolean ;
    begin
        {
            a stub
        }
    end ;

    Procedure DoIt (sIp, eIp, DNS : String) ;
    {
        working part of the program
    }
    begin
        {
            a stub
        }
    end ;

begin
    if ParamCount &amp;gt;= 3 then begin
        if
            (sIp_Valid(ParamStr(1)))
        and
            (eIP_Valid(ParamStr(2)))
        and
            (DNS_Valid(ParamStr(3)))
        then
            DoIt (ParamStr(1),ParamStr(2), ParamStr(3))
        else
            WriteLn ('Invalid Parameter!')
    end
    else
        WriteLn ('Not enough parameters!')
end.
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Here the "main" part of the program merely does all the housekeeping and then turns the actual work over to the procedure &lt;strong&gt;DoIt&lt;/strong&gt;.  The first thing it does is to check whether there are at least three parameters on the command line.  If there are more then the extra ones are ignored.  If there are fewer then presumably the program does hot have enough data to do its job and the program terminates with the message 'Not enough parameters!'  I prefer to put the termination message in the &lt;strong&gt;else&lt;/strong&gt; part of the &lt;strong&gt;if&lt;/strong&gt; statement instead of using &lt;strong&gt;halt&lt;/strong&gt; or &lt;strong&gt;exit&lt;/strong&gt; to bug out.  Whatever.&lt;br /&gt;
&lt;br /&gt;
Validity of each parameter is embodied in three boolean functions &lt;strong&gt;sIp_Valid, eIp_Valid&lt;/strong&gt; and &lt;strong&gt;DNS_Valid&lt;/strong&gt;.  This assumes that the tests for each parameter are unique.  If they are the same then one Function could be defined and called thus:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
        if
            Valid(ParamStr(1))
        and
            Valid(ParamStr(2))
        and
            Valid(ParamStr(3))
        then
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
I've assumed that the values of sIp, eIP and Dns are simply ParamStr(1), ParamStr(2) and ParamStr(3).  If not then functions for converting the parameters can be written and called:&lt;br /&gt;
&lt;pre class="sourcecode"&gt;
            DoIt (eIpVal(ParamStr(1)), sIpVal(ParamStr(2)), DnsVal(ParamStr(3)) ;        
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Approaching it this way the main part of the program is only 15 lines and the complexities are shuffled off to other procedures and functions where the can be handled one at at time.  The procedure &lt;strong&gt;DoIt&lt;/strong&gt; can repeat this strategy and ultimately the entire program can consist of simple, easily understood modules.&lt;br /&gt;
&lt;br /&gt;
Actor&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/pasprog/373258/373297/re-more-efficient-way-of-checking-parameters/#373297</guid>
      <pubDate>Thu, 10 Jul 2008 00:17:09 -0700</pubDate>
      <category>Pascal</category>
    </item>
    <item>
      <title>Re: More efficient way of checking parameters</title>
      <link>http://www.programmersheaven.com/mb/pasprog/373258/373319/re-more-efficient-way-of-checking-parameters/#373319</link>
      <description>Thanks. That was exactly the kind of feedback I was looking for. You guys are awesome!&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/pasprog/373258/373319/re-more-efficient-way-of-checking-parameters/#373319</guid>
      <pubDate>Thu, 10 Jul 2008 17:39:13 -0700</pubDate>
      <category>Pascal</category>
    </item>
  </channel>
</rss>