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

please help what is wrong with this cgi perl survay script

Status
Not open for further replies.

dd7

Programmer
Sep 29, 2002
7
0
0
US
i could not read from the dat file if there is anything wrong with the if statment i can fix it put what is wrong with reading and writing to the file please



use CGI qw:)standard);
print header();
print start_html("CGI-Perl Programming voting response" .
" Response, using CGI.pm");


# read input fields from the html page
my $firstn = param("First");
my $lastn = param("Last");
my $lang = param("language");

# open and lock the file
open(PL,&quot;<hh.dat&quot;);
flock(PL, 2);

# read the count from the file
$count = <PL>;
chomp ($count);
{
@array = split(/ /,$count); <<<< somthing is wrong here
}
$C_count = $array[0]; <<<< if i try this with jsut perl
$Java_count = $array[1]; <<<< interpter it works just this part
$Ada_count = $array[2];
$Perl_count = $array[3];
$Cobol_count = $array[4];
$Fortran_count = $array[5];

print &quot;$C_count&quot;; <<<< here i just want to see if i can get
reply with the correct no put it is wrong


if ($lang == &quot;C&quot;)
{
$C_count =$array[0]+1;
}
else
{
$C_count =$array[0];
}

if ($lang == &quot;Java&quot;)
{
$Java_count =$array[1]+1;
}
else
{
$Java_count =$array[1];
}

if ($lang == &quot;Ada&quot;)
{
$Ada_count =$array[2]+1;
}
else
{
$Ada_count =$array[2];
}

if ($lang == &quot;Perl&quot;)
{
$Perl_count =$array[3]+1;
}
else
{
$Perl_count =$array[3];
}

if ($lang == &quot;Cobol&quot;)
{
$Cobol_count =$array[4]+1;
}
else
{
$Cobol_count =$array[4];
}

if ($lang == &quot;Fortran&quot;)
{
$Fortran_count =$array[5]+1;
}
else
{
$Fortran_count =$array[5];
}




flock (PL,8);
close (PL);

# reopen and write to the file --
open(PL,&quot;>plsurv.dat&quot;);
flock(PL,2);
print PL &quot;$C_count $Java_count $Ada_count $Perl_count $Cobol_count $Fortran_count&quot;;
flock(PL,8);
close (PL);



print &quot;$C_count&quot;; <<<here too wrong response




print &quot;<h3>&quot;;
print &quot;Thanks $firstn , for your input !&quot;;
print &quot;</h3>&quot;;
print br;print br;

print &quot;</h2>&quot;;
print &quot;here is the votes&quot;;
print &quot;</h2>&quot;;
print br;

print &quot; C/C++ : $C_count&quot; ;
print br;
print &quot; Java : $Java_count&quot; ;
print br;
print &quot; Ada : $Ada_count&quot; ;
print br;
print &quot; Perl : $Perl_count&quot; ;
print br;
print &quot; Cobol : $Cobol_count&quot; ;
print br;
print &quot; Fortran : $Fortran_count&quot; ;



print end_html;

 
Hello,

You need to have the full path to the file you want to open listed in you statements. I will demonstrate on the first opening of the file
$pathtodatafiles='/path/to';
$datafilename='hh.dat';
open (PL, &quot;<$pathtodatafiles/$datafilename&quot;)||die &quot;Unable to open input file&quot;;#<--Error cathing

# read the count from the file
$count = <PL>;
chomp ($count);
{#<---This makes a block of code but you have no way for
#you program to enter the block so simply omit it
@array = split(/ /,$count); <<<< somthing is wrong here
}


HTH
 
thank you so much
one more thing in IIS 5.1 what should i use in the top of the perl cgi script
i mean i use this
#!/usr/local/gnu/bin/perl --

do you know what should it be in IIS 5.1
#....................

thank you again!
 
another thing :( please

the if statmet is incrementing all of them not just the one chosen

why

thanx
 
You should use the CGI::Carp module and direct errors to your browser when debugging.
Code:
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
Also, as Haunter suggested, check the success of open() statements. A failed open will set the $! variable with the reason for the failure so its usefull to put this in the die statement.
Code:
open (PL, &quot;<$pathtodatafiles/$datafilename&quot;)||die &quot;Unable to open input file: $!&quot;;#<--Error catching
If the open fails then CGI::Carp will catch the die and output the error to your browser.

Also, in your if statments you have to use string comparisons, 'eq', not numeric comparison, '=='.
Code:
if ($lang eq &quot;Java&quot;)

As an optimization, you can get rid of your if statements by assigning the index values of @array for each language to a hash and then just to a hash lookup on $lang to increment the array. Modifying your code slightly:
Code:
my %langtype = (    C       => 0,
                    Java    => 1,
                    Ada     => 2,
                    Perl    => 3,
                    Cobol   => 4,
                    Fortran => 5,
            );

# open and lock the file
open(PL,&quot;<hh.dat&quot;) or die &quot;Can't open for input: $!&quot;;
flock(PL, 2);

# read the count from the file
my $count = <PL>;
chomp ($count);

my @array = split(/ /,$count);

print &quot;C count: $array[ $langtype{'C'} ]\n&quot;;

$array[ $langtype{ $lang } ]++; # Replaces huge list of if statements

flock (PL,8);
close (PL);

# reopen and write to the file --
open(PL,&quot;>hh.dat&quot;) or die &quot;Can't open for output: $!&quot;;
flock(PL,2);
print PL &quot;@array&quot;;
flock(PL,8);
close (PL);

print &quot;C count: $array[ $langtype{ $lang } ]\n&quot;;
This does exactly the same thing as your code just without all the if's and unnecessary assignments.

As for your unexpected results, try printing the value of $count to see if it is what you expect it to be. (Or post it here)

Haunter:

{#<---This makes a block of code but you have no way for
#you program to enter the block so simply omit it
@array = split(/ /,$count); <<<< somthing is wrong here
}

Your comment is wrong. This is valid Perl syntax. It is common to use blocks like this to localize lexical variables, among other reasons. In this particular case it is pointless to use them, but the code contained in the block get executed none the less.

jaa
 
I really appreciate you help thanks a lot :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top