TypeA Suspended 3327 Posts user info edit post |
For the love of god, can someone show me how to do this shit correctly?
I understand what references and pointers are from the C world and I can do it just fine for regular lists (arrays) in perl, but I am fucking something up with hashes and its proceeding to rape me.
the call:
%x_hash=&X_BLOCK($fileLines,\@inputFile,\%x_hash);
the routine:
sub X_BLOCK {
($lineCount, $inFile, %theHash) = @_; for($line= 1; $line<=$lineCount; $line=$line+1) { # foreach $key (keys (%theHash)) { if($theHash{$key}[1] == 1) {next}; if(@$inFile[$line] =~ /($key)(.*)($theHash{$key}[0])/) { print "Key is $key: value is %$theHash{$key}[0]\n"; print "Key is $key value is $2.\n"; $theHash{$key}[1] = 1; next; } } } return %theHash; }
In another routine I am able to read the the contents of a file into an array, pass it out, and sucessfully pass it back in to this routine. I can't get it right for the hash.
[Edited on August 5, 2005 at 12:09 PM. Reason : *hash]8/5/2005 12:09:09 PM |
darkone (\/) (;,,,;) (\/) 11610 Posts user info edit post |
It's all magic anyway. Just throw some goat's blood on it, that will fix it. 8/5/2005 12:32:07 PM |
TypeA Suspended 3327 Posts user info edit post |
Fuck I had an epiphany finally
sub TEST {
$thehash = shift; foreach $key (keys (%$thehash)) { # $alpg_hash{$key}[$ver+1] = $2; #Store the information in the correct slot in the hash of arrays $alpg_hash{$key}[1] = 1; # indicate that we already found this one on this iteration print "Key is $key: value is $$thehash{$key}[0]\n"; } }
What I was confused about was the
foreach $key (keys (%$thehash)) part was correctly getting the keys. But the
print "Key is $key: value is %$thehash{$key}[0]\n"; part was giving me: Use of uninitialized value in concatenation (.) or string at .... error.
Durr...It worked for the keys because the keys function expects a hash. The second pat expects a scalar and I was trying to dereference a scalar to a hash.
When dereferncing to a scalar like so
$$thehash{$key}[0] it works.
You would think 1 year wouldn't be too long to forget how pointers and things work. Glad I'm sticking to the electrical side of things.8/5/2005 1:21:01 PM |