Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Internal Server Error - stuck on syntax error

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
catonthecouchproductions
Asked:
catonthecouchproductions
  • 7
  • 4
  • 2
1 Solution
 
BioICommented:
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
 
catonthecouchproductionsAuthor Commented:
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
 
BioICommented:
can you print out the $date-value, to get an idea how this looks like?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
catonthecouchproductionsAuthor Commented:
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
 
BioICommented:
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
 
catonthecouchproductionsAuthor Commented:
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
 
BioICommented:
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
 
catonthecouchproductionsAuthor Commented:
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
 
catonthecouchproductionsAuthor Commented:
Great help!
0
 
TintinCommented:
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
 
catonthecouchproductionsAuthor Commented:
Wow...how does that work? That little bit of code compared to that other stuff?
0
 
catonthecouchproductionsAuthor Commented:
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
 
TintinCommented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 7
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now