User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » Perl - passing has references Page [1]  
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
(\/) (;,,,;) (\/)
11605 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 it. For the time being I'm just letting the sub access a global, but I hate doing it that way.

8/5/2005 12:56:26 PM

scud
All American
10803 Posts
user info
edit post

& is reference operator

8/5/2005 12:59:31 PM

Incognegro
Suspended
4172 Posts
user info
edit post

http://www.google.com/search?hl=en&lr=&q=how+to+pass+a+hash&btnG=Search

ProgrammingTalk - How do I pass a hash reference to a subroutine
http://www.programmingtalk.com/archive/index.php/t-19136.html

8/5/2005 1:00:16 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

 Message Boards » Tech Talk » Perl - passing has references Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.