Help Putting data from an file that Perl reads into a JavaScript array 
Author Message
 Help Putting data from an file that Perl reads into a JavaScript array

I have been trying for the past week to get information from a file that Perl
reads into a JavaScript array.

Can anyone help me figure this out? As of right now I am getting a syntax error
at the  open command.

#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<html><h1>Hello!</h1></html>\n";

print <<ENDOFHTML

script <language = "javascript" >

var questions = [

ENDOFHTML

open (QUESTIONS, "questions.txt");
$line = <QUESTIONS>;
chop($line);
chop($line);
print "\"$line\"";
while($line = <QUESTIONS>)
{
    chop($line);
    chop($line);
    print ",\"$line\"";

Quote:
}

print <<END

];

var i;

for (i = 0; i < questions.length; i++)
{
  alert("The value of question is " + questions[i]);

Quote:
}

</script>

</html>

END

Jehan

To e-mail, remove NOJUNK



Fri, 05 Sep 2003 11:16:57 GMT  
 Help Putting data from an file that Perl reads into a JavaScript array
Falc2199 chose 19 Mar 2001 03:16:57 GMT to say this:

Quote:
>...

>Can anyone help me figure this out? As of right now I am getting a syntax
error
>at the  open command.

>...

>open (QUESTIONS, "questions.txt");

I don't see a syntax error there. What was the error message? You
should include some error-checking, anyway:

open(QUESTIONS, '<./questions.txt') || die "Can't open questions.txt: $!";

This will at least tell you whether and/or why open() is failing.

BTW, why aren't you using -w, strict, and CGI?

HTH,

Cheers
Damian



Just another Perl Hacker



Fri, 05 Sep 2003 13:21:15 GMT  
 Help Putting data from an file that Perl reads into a JavaScript array


Quote:
> I have been trying for the past week to get information from a file
that Perl
> reads into a JavaScript array.

> Can anyone help me figure this out? As of right now I am getting a syntax
error
> at the  open command.

> #!/usr/bin/perl

> print "Content-type: text/html\n\n";
> print "<html><h1>Hello!</h1></html>\n";

Bad HTML, you use a closing HTML tag later also.

Not having a closing semicolon

Quote:
> print <<ENDOFHTML

here--------------------^^^^,

means the print statement doesn't end until:

Quote:
> script <language = "javascript" >

> var questions = [

> ENDOFHTML

> open (QUESTIONS, "questions.txt");

here-----------------------------------^^^.
<snip>
That's why you get the syntax error at "open()".

should be:  print <<ENDOFHTML;

Try this,

#!/usr/bin/perl -wT

use strict;

my $html = q~<html>
<head>
<title>Test</title>
<script language="JavaScript" type="text/javascript">
~;

open(QFILE, 'questions.txt') || die "Could not open questions.txt: $!";
flock(QFILE, 2) unless ($^O =~ /MSWin32/);
my $cnt = 0;
my $array_vals;
while (<QFILE>) {
chomp;
$array_vals .= "questions[$cnt] = \"$_\"\;\n";
$cnt++;

Quote:
}

close(QFILE);

$html .= qq~var questions = new Array($cnt)\;
$array_vals
function showArray() {
for (i=0;i<questions.length;i++) {
alert('The value of questions['+i+'] is '+questions[i]+'.')\;

Quote:
}
}

</script>
</head>
<body>
<a href="javascript:showArray()\;">Show Array</a>
</body>
</html>~;

print "Content-type: text/html\n\n";

print $html;



Fri, 05 Sep 2003 17:23:48 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. HELP: putting an @array into an %array???

2. reading javascript arrays

3. newbie question: How to read a dir and put contents into an array

4. Help Reading data in text files

5. Need a Help for Reading Data From simicolon delimited file

6. Newbie need help with reading data file...

7. Help reading data from file

8. Help, reading hash data from a file.

9. Help reading a binary data file (scientific)

10. put data from web in a file

11. extracting data from a file to be put in a variable

12. put data from web in a file

 

 
Powered by phpBB® Forum Software