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

Pearl File I/O and Java 1

Status
Not open for further replies.

MinnisotaFreezing

Programmer
Jun 21, 2001
120
KR
Hi, sorry for the very simple question, but I can't seem to get anywhere with it. Here goes:

I want to read a small text file with CGI and output the array to a JavaScript array.

I have this:

#!/usr/bin/perl
open(INFO, "names_file.txt"); # Open db for reading and display
@array=<INFO>;
close (INFO);

great, now the tutorial I read shows how to print to screen by parsing the array and such. But I don't want to print to screen, I want to load the data from @array into a JavaScript array, and then parse out with Java because I don't want the data displayed all at once. I wan't the CGI script to run, get all the data to memory(Its small)then access the data 1 at a time.

Some psudo-code:

//My Script called read.pl
#!/usr/bin/perl
open(INFO, &quot;names_file.txt&quot;); # Open db for reading and display
@array=<INFO>;
close (INFO);
return @array;

//My Page
<script Language=&quot;JavaScript&quot;>
var Source = new Array
Source = /cgi-local/axs/read.pl


Maybe a better way would be to dump the output of the CGI into a hidden form, and then read that into my array, but I can't even do that.

I realize this not the right way to do all this, but my page is done with the exception of this problem, and it is in JavaScript. I've looked on the web for a long time at CGI tutorials, but they all show output to screen. I understand CGI runs all at once, and that trying to catch out put 1 line at a time probably won't work. If I could only get a pointer to @array, but no.

Anyway, any help is of course appriciated.
 
In read.pl,
Code:
my $js_code = qq~var Source = new Array(~;
for (my $x = 0; $x < scalar @array; $x++) {$js_code .= qq~$array[$x],~;}
$js_code ~= s/,$//; # remove the trailing comma
$js_code .= qq~);~; # close the JS array
print $js_code; # output the JS code
And in HTML,
Code:
<script Language=&quot;JavaScript&quot; src=&quot;read.pl&quot;></script>
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Thank you so much for the help, this looks like exactly what I need.

I have 1 more question however. I can't seem to get at the data in JS. I try like this:
<script Language=&quot;JavaScript&quot; src = &quot; alert(js_code)</script>

that doesn't work. And the way I think it should be done:

<script Language=&quot;JavaScript&quot; src = &quot; </script>
<script Language=&quot;JavaScript&quot;>
alert(js_code)

tells me js_code is unkown. Could you show me how to get at this data in my JS? Assum just a test case, where my text file contains 3 names, and all the HTML does is print out an alert with all the names.

Thanks again
 
That's because the JavaScript array is named &quot;Source&quot;. The variable $js_code is a Perl variable which does not get read in by the JavaScript. When you execute read.pl, this is the output:
Code:
var Source = new Array(lines,from,text,file);
By making this a &quot;src&quot; to your JavaScript, this line is executed within your browser session and creates the array named &quot;Source&quot;. When you output this array in JavaScript, you're going to want to use a loop. The alert() function will probably tell you that it is an &quot;object&quot; or &quot;array&quot; rather than printing out the array elements.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Thanks for that answer. That was exactly what I was looking for, how do I get at that data. Its the array called Source.
 
Hi, I'm back again. I've worked with this for a couple of days, and just can't get it to run.

I made a test page to work with at
This page consists of
Code:
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
  <script Language=&quot;JavaScript&quot; src=&quot;[URL unfurl="true"]http://www.dynacomproducts.com/cgi-local/axs/read.pl&quot;>[/URL]
  alert(Source);
  document.write(Source);
  </script>
  <script Language=&quot;JavaScript&quot;>
 document.write('<a href=&quot;\/cgi-local\/axs\/readtest.pl&quot;>run it</a>');
 document.write('Newer Version');
  </script>
</body>
</html>
When I load this page, neither the alert or document.write fire, which tells me I did something wrong. I have narrowed it down as much as I can, I wrote readtest.pl, which runs, and reads the file, and outputs the content. Both pearl scripts are in the same dir on the server, and both include identical headers.

The script you wrote for me:
Code:
#!/usr/bin/perl


open(INFO, &quot;names_file.txt&quot;); # Open db for reading and display
@array=<INFO>;
close (INFO); 
my $js_code = qq~var Source = new Array(~;
for (my $x = 0; $x < scalar @array; $x++) {$js_code .= qq~$array[$x],~;}
$js_code ~= s/,$//; # remove the trailing comma
$js_code .= qq~);~; # close the JS array
print $js_code; # output the JS code

The script I wrote(Kinda):
Code:
#!/usr/bin/perl


# Print the CGI response header, required for all HTML output
# Note the extra \n, to send the blank line
print &quot;Content-type: text/html\n\n&quot; ;
open (Names, &quot;names_file.txt&quot;) || die &quot;couldn't open the file!&quot;;


   while ($record = <Names>) {
      print $record;
   }

   close(Names);

# Print the HTML response page to STDOUT
print <<EOF ;
<html>
<head><title>CGI Results</title></head>
<body>

</body>
</html>
EOF

exit ;
Could you please tell me where I went wrong?
Oh, I have confirmed many times both files exist, and they both have excecute access on the server.
Thanks in advance
 
Try this:
Code:
#!/usr/bin/perl

use strict;

# Get file info
open (Names, &quot;names_file.txt&quot;) || die &quot;Could not open names_file.txt: $!&quot;;
my @names = <Names>;
close(Names);

# Print the CGI response header, required for all HTML 
print &quot;Content-type: text/html\n\n&quot; ;

# Print the HTML page
print qq~
<html>
 <head>
  <title>cgi test</title>
 </head>
 <body>
  <script language=&quot;JavaScript&quot;>
   ~;

# Print the file info to JavaScript
my $js_code = &quot;var Source = new Array(&quot;;
for (my $x = 0; $x < scalar @names; $x++) {$js_code .= qq~&quot;$array[$x]&quot;,~;}
$js_code ~= s/,$//; # remove the trailing comma
$js_code .= qq~);~; # close the JS array
print $js_code; # output the JS code

# Finish the HTML
print qq~
   var output = &quot;&quot;;
   for (var i = 0; i < Source.length - 1; i++) {output += Source[i] + &quot;,&quot;;}
   output += Source[Source.length - 1];
   alert(output);
  </script>
 </body>
</html>
~;

exit ;

I haven't actually tested it, so there may be minor syntax errors. But it should give you the general idea. Look at the source of this HTML page when you output it. BTW, I'm assuming that the JS array is what is wanted, otherwise just printing out a string would be easier for display purposes. But if you use an array, then you must loop through it and create a string in order to print it out.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Thank you very much. An array is exactly what I am looking for. In fact, I think I mislead you with my test example, I don't want to print any html with cgi, only read a file, and then put it into an array so when a user clicks on next, it cycles through the array and displays the content. Like, each comma delimited record of the txt file will be a picture;description pair, which will change the display on the screen.

Anyway, thanks for the help, this should give me a base to work off of.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top