mfrappie
asked on
creating comma separated values with perl ??
I have a set of forms with scripts that save the form input data to a file. This file is later downloaded to my pc so I can transfer the data to a database program (Access). Is there any way to convert this form data to comma separated values with the perl cgi script before the data is saved to the file? That way it can be imported directly to my Access database.
ASKER
SCRIPT # 1:
#! /usr/bin/perl
print "Content-type: text/html\n\n";
$rid = $$ . "-" . time();
print <<end;
<HTML><HEAD><TITLE>Survey Introduction</TITLE></HEAD >
<BODY background="whitebak.gif" bgcolor="#ffffff" text="#000080" link="#0064ff" vlink="#00a800" alink="#800000">
<FORM ACTION="demog.cgi" METHOD=POST>
<INPUT TYPE="hidden" NAME="respondent_id" VALUE="$rid">
[*HTML DELETED*]
<INPUT TYPE="submit" VALUE="START SURVEY">
</FORM>
</body>
</html>
end
SCRIPT # 2:
#! /usr/bin/perl
read(STDIN, $message, $ENV{'CONTENT_LENGTH'});
@pairs=split(/&/, $message);
foreach $pair (@pairs) {
($name,$value)=split(/=/,$ pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9] )/pack("c" ,hex($1))/ eg;
$FORM{$name} = $value;
}
$rid=$FORM{"respondent_id" };
open(LOGFILE, ">survey_results/$rid") || die "Can't open logfile!\n";
print LOGFILE "$message\n";
close (LOGFILE);
print "Content-type: text/html\n\n";
print <<end;
<HTML><HEAD><TITLE>Demogra phics</TIT LE></HEAD>
<BODY background="whitebak.gif" bgcolor="#ffffff"
text="#000080" link="#0064ff" vlink="#00a800" alink="#800000">
<FORM ACTION="fam_hx.cgi" METHOD=POST>
<INPUT TYPE="hidden" NAME="respondent_id" VALUE="$rid">
[*HTML DELETED*]
<INPUT TYPE="submit" VALUE="Go to the next section">
<INPUT TYPE="reset" VALUE="Reset/Re-enter data">
</FORM>
</body>
</html>
end
There are more scripts that are the same as these that continue to add form data to the logfile.
#! /usr/bin/perl
print "Content-type: text/html\n\n";
$rid = $$ . "-" . time();
print <<end;
<HTML><HEAD><TITLE>Survey Introduction</TITLE></HEAD
<BODY background="whitebak.gif" bgcolor="#ffffff" text="#000080" link="#0064ff" vlink="#00a800" alink="#800000">
<FORM ACTION="demog.cgi" METHOD=POST>
<INPUT TYPE="hidden" NAME="respondent_id" VALUE="$rid">
[*HTML DELETED*]
<INPUT TYPE="submit" VALUE="START SURVEY">
</FORM>
</body>
</html>
end
SCRIPT # 2:
#! /usr/bin/perl
read(STDIN, $message, $ENV{'CONTENT_LENGTH'});
@pairs=split(/&/, $message);
foreach $pair (@pairs) {
($name,$value)=split(/=/,$
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9]
$FORM{$name} = $value;
}
$rid=$FORM{"respondent_id"
open(LOGFILE, ">survey_results/$rid") || die "Can't open logfile!\n";
print LOGFILE "$message\n";
close (LOGFILE);
print "Content-type: text/html\n\n";
print <<end;
<HTML><HEAD><TITLE>Demogra
<BODY background="whitebak.gif" bgcolor="#ffffff"
text="#000080" link="#0064ff" vlink="#00a800" alink="#800000">
<FORM ACTION="fam_hx.cgi" METHOD=POST>
<INPUT TYPE="hidden" NAME="respondent_id" VALUE="$rid">
[*HTML DELETED*]
<INPUT TYPE="submit" VALUE="Go to the next section">
<INPUT TYPE="reset" VALUE="Reset/Re-enter data">
</FORM>
</body>
</html>
end
There are more scripts that are the same as these that continue to add form data to the logfile.
ASKER
Adjusted points to 195
There's no doubt something very quick and dirty you can do here, just to get a CSV file but not do any data validation. Does your CGI program already do data validation of some kind, or are you discarding incomplete/incorrect records once you get them into Access?
ASKER
There's no validation. I'm pretty inexperienced, i.e., I'm not a programmer. I'm not even real sure what you mean by data valadation. The whole script is listed above except for the html questions. Thanks
You really need to do something more than "print LOGFILE $message" to your file.
in your variable-splitting code... you are breaking up the message into separate variables. You need to use something else in the place of the 'print LOGFILE $message' that prints out each of these variables.
Lets say in your form, you had an <INPUT name=answer1> and an <INPUT name=answer2>. THEN you should have the variables:
$FORM{'answer1} and $FORM{'answer1'}
to play with. You could replace your "print LOGFILE $message" with something else like this:
print LOGFILE "$FORM{'answer1'}, $FORM{'answer2'}";
or better yet
print LOGFILE join(', ', values(@FORM));
which have the problem of coming out in some strange order.
in your variable-splitting code... you are breaking up the message into separate variables. You need to use something else in the place of the 'print LOGFILE $message' that prints out each of these variables.
Lets say in your form, you had an <INPUT name=answer1> and an <INPUT name=answer2>. THEN you should have the variables:
$FORM{'answer1} and $FORM{'answer1'}
to play with. You could replace your "print LOGFILE $message" with something else like this:
print LOGFILE "$FORM{'answer1'}, $FORM{'answer2'}";
or better yet
print LOGFILE join(', ', values(@FORM));
which have the problem of coming out in some strange order.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I found a small script that does something very similar to your suggestion. Between your answer and that script I should be able to make something work. If anyone else looks at this answer and wants to take a look at a script that does this Take a look at the SimpleBase script by Dave Palmer at:
http://www.upstatepress.com/dave/perl.shtml
http://www.upstatepress.com/dave/perl.shtml
-julio