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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hashes

Status
Not open for further replies.

Loon

Programmer
May 24, 2000
100
GB
Hi,<br>&nbsp;&nbsp;&nbsp;I've just started trying to use hashes. Basically I want to use a modules package name as the index to the hash so that when a trace function is called it checks to ensure that tracing has been turned ON in that package. <br><br>&nbsp;&nbsp;&nbsp;So I have an initialisation function whereby I read a text file of the form:<br><br>package1=ON<br>package2=ON<br>package3=OFF<br>...etc<br><br>&nbsp;&nbsp;&nbsp;&nbsp;And then these are split on the = and the hash set up:<br><br>e.g.<br><FONT FACE=monospace>%myhash = ();<br>while(&lt;TRACECONFIGFILE&gt;)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;($package_name, $trace_value) = split(/=/,$_);<br>&nbsp;&nbsp;&nbsp;&nbsp;$myhash{'$package_name', $trace_value);<br>}</font><br><br>&nbsp;&nbsp;&nbsp;So when the trace call is made, inside the trace function I want to do something like:<br><br><FONT FACE=monospace>if ($myhash{$package_name} eq &quot;ON&quot;)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;trace a message to a file<br>}<br>else<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;don't do anything<br>}</font><br><br>&nbsp;&nbsp;&nbsp;Is doesn't work at the moment as in the <i>if</i> isn't returning true, even when the text file sets that hash to &quot;ON&quot;...<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Hopefully that made sense! Really I just need to know how to use a scalar as the name of a hash key.. is this even possible?<br><br>Thanks for staying with me!<br>Loon
 
$myhash{'$package_name', $trace_value);<br><br>It may be to much to hope that it is this simple, but, <br>the brace is closed with a paren. <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Hmmmm....<br><br><FONT FACE=monospace><br> while(&lt;INF&gt;)<br> {<br> &nbsp;&nbsp;&nbsp;&nbsp;($package_name,$trace_value) = split(/=/,$_);<br> &nbsp;&nbsp;&nbsp;&nbsp;$tracing_area{'$package_name',$trace_value);<br> }<br></font><br><br>gave me:<br><br>syntax error at TRACING.pm line 54, near &quot;$trace_value)&quot;<br>Missing right bracket at TRACING.pm line 127, at end of line<br>BEGIN failed--compilation aborted at htc_parser.pl line 15.<br><br>Am I doing it right?! :-/<br>Cheers<br>Loon
 
I'm not familiar with building a hash in the manner you appear to be using.....<br>If it will work that way, you will need to close the brace with a brace.&nbsp;&nbsp;As written, you are closing a brace with a paren.<br><br>I add pairs to hashes like....<br><FONT FACE=monospace>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value<br>$tracing_area{$package_name} = $trace_value;<br></font><br><br>also, I think that enclosing the variables in your braces in single quotes will prevent then from being evaluated.&nbsp;&nbsp;Instead of getting the value contained in $package_name you're getting the string '$package_name', including the dollar sign. <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top