Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wierd sub-routine argument values

Status
Not open for further replies.

Loon

Programmer
May 24, 2000
100
GB
It might just be the norm (and as yet I haven't encountered it in my perl travels ) but this seems strange to me and is also annoying. I am writing a sub-routine and want to check the 2 args I pass to it e.g.:<br><FONT FACE=monospace><br>#!/usr/bin/perl<br><br>$param = 2;<br>&check_params($param);<br><br>sub check_params<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($_[0] gt &quot;10&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;Perl thinks $_[0] is greater than 10\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>}</font><br><br>And when this run I get the line:<br><font color=red><FONT FACE=monospace><br>&lt;298-&gt; ./paramtest.pl<br>Perl thinks 2 is greater than 10<br>&lt;299-&gt;<br></font></font><br><br>What's going on?<br><br>Cheers<br>Loon
 
What happens if you use the &quot;&gt;&quot; operator to test the values?&nbsp;&nbsp;What I think is happening is that perl is doing an ASCII comparison on the values, and in that case &quot;2&quot; will be greater than &quot;10&quot;.<br><br>To illustrate this, create a file that contains values from 1 to 15 in ascending order.&nbsp;&nbsp;Then run <FONT FACE=monospace>sort myfile</font>.&nbsp;&nbsp;See how it gets ordered as &quot;1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9&quot;?&nbsp;&nbsp;That's what perl is doing, and so &quot;2&quot; is greater than &quot;10&quot;.<br><br>Use the &quot;&gt;&quot; operator and all should be OK. <p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits
 
It does indeed, if I'd been more with it I might even have guessed about the implicit ASCII conversion...<br><br>I hate wednesdays.<br><br>Thanks!<br>Loon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top