Devlin Technical Consulting Home
325 S. Washington Ave. #339, Kent, WA 98032
PH: (253)272-5743 email: jdevlin@stadiumdistrict.com

Collect HTML form data to a hash in your Perl CGI script

 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# File:		h.pl
# Written by:	Devlin Technical Consulting
#		Tacoma, WA
# Description:
# This hash maker script accepts input from a form.
# The query string will be split into a format that
# can be saved as a hash that can be searched for 
# the parameters in any order.
#
# This way of getting unknown quantity parameters can 
# be used when processing multiple forms with the same
# script.  Get the parameters on opening your script 
# then figure out what to do with them in each subroutine.

# This is a simple script, it does not address URL
# encoding and decoding.  The CGI.pm param function
# does all of these things for you.  See that example
# in a different section of the is web site.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# Revision 1	9/12/00		Initial
# # # # # # # # # # # # # # # # # # # # # # # # # # # # 


sub form_to_hash{

=head1 form_to_hash

This method is passed a string scalar that is the passed
items from an HTML get action. The string will have = and
& signs between the words. This subroutine removes those 
symbols and puts the words into a hash and returns the 
hash.
        Example:
	my $sample_data = 'you=me+bert%2Cernie%2Fred&yellow=yesterday%3Atomorrow';
	%form_data=form_to_hash($sample_data);
	
	sets %form_data to the following:
	keys:	values:
	you	me+bert%2Cernie%2Fred
       	yellow	yesterday%3Atomorrow

=cut

   	my $query= my $i= my $n= "";
	my $query_string= shift;
	my @temp= "";
	my %hash= ();
        $query_string =~s/[=&]/ /g;      	#d3 Puts spaces between items.
	@temp = split / /, $query_string;       #d3 Turns a space delimited scalar into an array.
	while ($key = shift @temp){		#d3 Assigns key and value pairs from @temp
		$value=shift @temp;
		$hash{$key}=$value;
	}
	return %hash;
}





 
     
   







Page Modified 2/16/00