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

wondering about HASH(0x80d1eac) 1

Status
Not open for further replies.

theEclipse

Programmer
Dec 27, 1999
1,190
US
Hello,

I am attempting to set up a perl script that will take in any form inputs and create a name-value associative array to hold the inputs.

The script works except for when I try to do an output to monitor the array. I use a foreach loop to do this.

My output is as follows:

HASH(0x80d1f00) =
firstName = Robert
lastName = Carpenter
favoriteColor = orange

I have only three fields, firstname and lastname and favorite color. so what is the "HASH ......" line?


I have the script online at:
and the script itself

any help is appreciated. if anybody wants the source code, please email me linkemapx@hotmail.com Robert Carpenter
questions? comments? thanks? email me!
linkemapx@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
It appears to be a hash reference stringified. Can you please post your code?

--jim
 
Of course! (and btw, thankyou for your help)

I highlited the section that outputs the error in red. Also, dont mind the comments, I am new to perl and trying to help myself remember what I am doing.
So what is a "hash reference"?

Code:
#!/usr/bin/perl --

print "Content-type: text/html\n\n"; #This line tells the browser that it will be getting a text/html output


#This if/else statment sets the $in variable to the search string be it method get or post
if ($ENV{'REQUEST_METHOD'} eq "GET") {	#If the request method equals 'get'
	$in = $ENV{'QUERY_STRING'};						#then pull the $in string from the query string.
} else {																#otherwise, (if the method equals 'post')
	$in = <STDIN>;												#then pull the $in string from the standard input variable.
}


#this replaces the spaces (because the browser will turn them into +'s for cgi
$noSpace=$in;
$noSpace=~s/\+/ /g;

#this replaces all of the %3d (=) with a %5e (^). if this is not done
#and the user had an equal sign as part of the input, then the code will
#see the words around that as a name-value pair.
$withoutEqual=$noSpace;
$withoutEqual=~s/%3d/%5e/gi;


#this converts the string to actual text instead of being ascii encoded (%3d into &quot;=&quot;)
$convertToASCII=$withoutEqual;
$convertToASCII=~s/%(..)/pack(&quot;c&quot;,hex($1))/ge;



#this splits up the clean input into an array with name and value in a slot
@fields = split (/&/, $convertToASCII);

#--------------------------------------------------------------------------#
#this loop goes through each of the slots in the @fields array and splits
#the data up into two parts, name and value. It then stores the name-value
#pairs into an associatve array.

%inputs = {}; #initalize the %inputs associatve array.
for ($i=0; $i<=$#fields; $i++){
	@tempNameValue=split(/=/,$fields[$i]);
	$inputs{@tempNameValue[0]}=@tempNameValue[1];
	#debugging outputs
	#print(&quot;\$inputs{@tempNameValue[0]}=@tempNameValue[1]<br>&quot;);
	#print(&quot;*********************************************<br>&quot;);
}
#--------------------------------------------------------------------------#


print <<END;
<html>
<head>
<title> a perl script input demo</title>
</head>
<body bgcolor=&quot;#000000&quot; text=&quot;#FFFFFF&quot;>
A perl demo's output script:<br>
$in<Br><Br>
Then we removed all the &quot;+&quot; signs and replaced them with spaces:<br>
$noSpace<br><br>
Lastly we converted all of the &quot;%20&quot; in to real characters.<br>
$convertToASCII<br><br>
And then we must organize all of the inputs into an associative array:<br>
END
Code:
foreach $i (keys (%inputs)){
	print &quot;$i = $inputs{$i} <br>&quot;;
}
Code:
print <<END;

</body>
</html>

END
Robert Carpenter
questions? comments? thanks? email me!
linkemapx@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
Hello Eclipse
does this help at all;
foreach $i (keys %inputs){
print &quot;$i => $inputs{$i} <br>&quot;; Nogs
[ponder]
 
I think your problem is actually up where you first declare %inputs. You say
Code:
%inputs = {};
When you use curly braces like that, you're actually making a reference to a hash. Always tack on the following to the top of every perl script:
Code:
use warnings;
use strict;
In this case, the warnings pick up the error. Since it appears you're doing this over the web, you'd have to have access to your web server logs to see stderr (I think I read something once about a way to append that to the bottom of the generated output, but don't remember).

The fix is
Code:
%inputs = ();
Using empty parentheses is an empty hash or array. Using {} or [] is an empty hash or array reference. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
nogs-
thanks for the input but I think that is the same as what I have already.

icrf-
thanks amillion man! that worked great. but I still have two questions if you dont mind.
1) is it necessary to declare (initialize?) variables in perl as it is in say C?
2) what is a hash refrence? is it like a null value?


thanks again! Robert Carpenter
questions? comments? thanks? email me!
linkemapx@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top