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

Help - Not a hash reference

Status
Not open for further replies.

grallaj

Instructor
Apr 1, 2004
4
US
I'm doing some small programming in perl to improve the way significant figures are tracked for webwork... Some of my functions are not working and it seems to be the way the hash functions are being passed.

Here is a snip of my code. The makeSFhash creates a hash from a number with the fields: raw, sf and rounded. Since my other functions did not seem to be working, I did a passHash test function to see if my other functions were getting the hashes correctly. They weren't. It always tells me in the passHash function that they are not hash references. Any ideas?

sub passHash {
my (%m) = shift;
my (%n) = shift;
my $h;
$h = $m{"raw"};
return $h;
}

sub makeSFhash {
my ($n) = @_;
my ($sf,$rounded);
$sf = CountSigFigs($n);
$rounded = FormatSigFigs($n,$sf);
return("raw"=>$n,"sf"=>$sf,"rounded"=>$rounded);}

$percent = random(2.01,9.99,.02);
%percenthash = makeSFhash($percent);

$mass = random(401,999,2);
%masshash = makeSFhash($mass);

$test = passHash(%percenthash,%masshash);

 
passing a hash to a sub by just saying %hash flattens it to a list. If you put two hashes side by side, they get flattened into one indistinguishable list. To make a reference, you could say [tt]passHash(\%percenthash,\%masshash);[/tt] and your passHash sub might look something like this:
Code:
sub passHash {
  my ([COLOR=red]$[/color]m) = shift;
  my ([COLOR=red]$[/color]n) = shift;
  my $h;
  $h = $m[COLOR=red]->[/color]{"raw"};
  return $h;
}

________________________________________
Andrew - Perl Monkey
 
You know... I'm pretty sure I tried that too. Actually, I think I copied and pasted the wrong code. Let me try it anyhow...
 
Here's my code:

sub passHash {
my ($ref1,$ref2) = @_;
my %hash1 = %$ref1;
my %hash2 = %$ref2;
my ($h);
$h = $hash1{"raw"};
return $h;
}

sub makeSFhash {
my ($n) = @_;
my ($sf,$rounded);
$sf = CountSigFigs($n);
$rounded = FormatSigFigs($n,$sf);
return("raw"=>$n,"sf"=>$sf,"rounded"=>$rounded);}

$percent = random(2.01,9.99,.02);
%percenthash = makeSFhash($percent);

$mass = random(401,999,2);
%masshash = makeSFhash($mass);

$test = passHash(\%percenthash,\%masshash);

Same result...
 
So what exactly are you running and what exactly is the problem/error msg? I ran that code (substituting dummy data for the unlisted subs) and it ran fine, no errors.

________________________________________
Andrew - Perl Monkey
 
Not a HASH reference at (eval 53) line 32.

That is my error message. I am running this from within webwork, an online homework service...
 
Umm...what line is line 32? There's no eval statements in anything you posted. You in the right version of the right file? Using strict and warnings?

________________________________________
Andrew - Perl Monkey
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top