Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Internal Server Error - stuck on syntax error

Posted on 2007-11-26
13
Medium Priority
?
208 Views
Last Modified: 2010-04-21
I have a script to modify the date and I am stuck on a simple syntax error on my page..Below is my code, any Putty said it was:

syntax error at date.cgi line 38, near "){"
Execution of date.cgi aborted due to compilation errors.

Any suggestions?

Thanks,
Ryan

#!/usr/bin/perl
# date.cgi - converts a numeric date to a string
use CGI qw(:standard);
print "Content-type: text/html\n\n";
 
# declare variables for date
my (@months);
my ($tmon);
my ($i);
my ($a);
my ($date);
my ($m); # month
my ($d); # day
my ($y); # year
 
 
# assign input item to variable
# initiate all variables either start at 0 or use param()
$date = param('date');
$m    = 0;
$d    = 0;
$y    = 0;
 
# array of months
@months   = ("January","February","March","April","May","June","July","August","September","October","November","December");
# tmon is 12
$tmon = @months;
$i    = 0;
$a    = 0;
 
#split date apart using split() on "/"
my @date = split("/", $date);
$m       = $date[0]; # array pos 0
$d       = $date[1]; # array pos 1
$y       = $date[2]  # array pos 2
 
# if for year add on "20 then XX"
if (length($y) == 2){
    $y= "20".$y;
}
# if for date
if (length($d) == 2){
    my @d = split("", $d);
    if ($d[0] == 0){
        $d = $d[1]
        }else{
        $d = join("", @d);
    }
}
#if for month - bring month array
if (length($m) == 2){
    my @m = split("", $m);
    if ($m[0] == 0){$m = $m[1]
        }else{
        $m = join("", @m);
    }
}
 
for ($i,$i<=$tmon,$i++)
{
    $a=$i-1;
    if ($m = $i){$m = $ms[$a];}
}
;
# print out final date
print "<b>The date is:</b> $m $d, $y\n";

Open in new window

0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 3

Expert Comment

by:BioI
ID: 20349717
you have to add a semicolon at the end of line 35
change:
$y       = $date[2]  # array pos 2
to
$y       = $date[2];  # array pos 2


0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20349727
Thank you after that I get - http://elan.champlain.edu/~rcoughlin32001/cgi-bin/date.cgi

Any suggestions how to get the output?

Thanks!

#!/usr/bin/perl
# date.cgi - converts a numeric date to a string
use CGI qw(:standard);
print "Content-type: text/html\n\n";
 
# declare variables for date
my (@months);
my ($tmon);
my ($i);
my ($a);
my ($date);
my ($m); # month
my ($d); # day
my ($y); # year
 
 
# assign input item to variable
# initiate all variables either start at 0 or use param()
$date = param('date');
$m    = 0;
$d    = 0;
$y    = 0;
 
# array of months
@months   = ("January","February","March","April","May","June","July","August","September","October","November","December");
# tmon is 12
$tmon = @months;
$i    = 0;
$a    = 0;
 
#split date apart using split() on "/"
my @date = split("/", $date);
$m       = $date[0]; # array pos 0
$d       = $date[1]; # array pos 1
$y       = $date[2];  # array pos 2
 
# if for year add on "20 then XX"
if (length($y) == 2){
    $y= "20".$y;
}
# if for date
if (length($d) == 2){
    my @d = split("", $d);
    if ($d[0] == 0){
        $d = $d[1]
        }else{
        $d = join("", @d);
    }
}
#if for month - bring month array
if (length($m) == 2){
    my @m = split("", $m);
    if ($m[0] == 0){$m = $m[1]
        }else{
        $m = join("", @m);
    }
}
 
for ($i,$i<=$tmon,$i++)
{
    $a=$i-1;
    if ($m = $i){$m = $ms[$a];}
}
;
# print out final date
print "<b>The date is:</b> $m $d, $y\n";

Open in new window

0
 
LVL 3

Expert Comment

by:BioI
ID: 20349937
can you print out the $date-value, to get an idea how this looks like?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20349950
Still the same result, I dont get anything
# print out final date
print "Date: $date";
print $date;
print "<b>The date is:</b> $m $d, $y\n";

Open in new window

0
 
LVL 3

Expert Comment

by:BioI
ID: 20349969
even if you print it *immediately* after $date = param('date')?
because in that case, there is something wrong with the input form that comes before this cgi-script I guess...
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20350007
If you try - http://elan.champlain.edu/~rcoughlin32001/date.html - only outputs some of it.
# assign input item to variable
# initiate all variables either start at 0 or use param()
$date = param('date');
 
# debug
print $date;

Open in new window

0
 
LVL 3

Accepted Solution

by:
BioI earned 2000 total points
ID: 20350012
by the way, you don't have to do something like this:
if (length($d) == 2){
    my @d = split("", $d);
    if ($d[0] == 0){
        $d = $d[1]
        }else{
        $d = join("", @d);
    }
}

perl will automatically interpret $d as an integer when it is used as the index of an array
This should work: [in the end, you used an non-existing array @ms instead of @months...]
#!/usr/bin/perl
# date.cgi - converts a numeric date to a string
use CGI qw(:standard);
print "Content-type: text/html\n\n";
 
# declare variables for date
my (@months);
my ($tmon);
my ($i);
my ($a);
my ($date);
my ($m); # month
my ($d); # day
my ($y); # year
 
 
# assign input item to variable
# initiate all variables either start at 0 or use param()
$date = param('date');
$m    = 0;
$d    = 0;
$y    = 0;
 
# array of months
@months   = ("January","February","March","April","May","June","July","August","September","October","November","December");
# tmon is 12
$tmon = @months;
$i    = 0;
$a    = 0;
 
#split date apart using split() on "/"
my @date = split("/", $date);
$m       = $date[0]; # array pos 0
$d       = $date[1]; # array pos 1
$y       = $date[2]  # array pos 2
 
# if for year add on "20 then XX"
if (length($y) == 2){
    $y= "20".$y;
}
 
$m = $months[$m];
 
 
# print out final date
print "<b>The date is:</b> $m $d, $y\n";

Open in new window

0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20350049
Thank you! That worked! So I didnt know that about how it will interpret it as an int as default! Thanks for all the help! Clearing this up for me.

So getting rid of that fixed it? Trying to understand my mistakes.

If you get some time I have one more easy one, but I am stuck - http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_22982316.html

Appreciate it
0
 
LVL 1

Author Closing Comment

by:catonthecouchproductions
ID: 31410966
Great help!
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20354713
Note that the code you accepted has a bug when it prints out the month.  Remember Perl arrays start at element 0, not 1.

Here's the corrected and simplified code:

#!/usr/bin/perl
# date.cgi - converts a numeric date to a string
use CGI qw(:standard);
print header;

my $date = param('date');

my @months   = qw(January February March April May June July August September October November December);

my ($mon,$day,$year) = split '/', $date;
$year="20$year" if (length($year) == 2);
my $month = $months[$mon-1];

print "<b>The date is:</b> $month $day, $year\n";
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20355296
Wow...how does that work? That little bit of code compared to that other stuff?
0
 
LVL 1

Author Comment

by:catonthecouchproductions
ID: 20355330
I have one more quick question:

>>my $month = $months[$mon-1];


Does that line take the array and subract one place? Due to how the arrays begin at 0? Or am I wrong?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20355366
You are correct in your assumption about the month array.

My code looks alot shorter due to:

1.  No comments.
2.  It's best practise to declare variables when they are used, not at the top of the script.
3.  Removed the need for a whole lot of intermediate variables that weren't needed.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question