473,770 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't Read Perl Array in JavaScript Using JSON

6 New Member
Hi Everyone,

I've been reading and searching the web for days trying to figure out how exactly you can access and read a Perl array or hash from within JavaScript. I've been reading that JSON is the way to go, but non explain exactly how. If you can help me out, I would really appreciate it.

Here's the scenario I have...

I have a perl file called index.pl and in this file I have the below code. I know that arrays are converted to strings including the array's subscript. The subscripts that I want to use are integers. For example, arr[0]. But, how can I use JSON (if it's the right choice) to access my perl array so I can loop through it and generate my HTML?

Expand|Select|Wrap|Line Numbers
  1. my @arr = ();
  2. push @arr, "arrValue1";
  3. push @arr, "arrValue2";
  4.  
  5. # Start HTML Generation
  6. print "HTTP/1.0 200 OK\n";
  7. print "Content-Type: text/html\n\n\n";
  8.  
  9. <html>
  10.   <head></head>
  11.   <body>
  12.     <script type="text/javascript" language="JavaScript">
  13.         for (var i = 0; i < 2; i++) {
  14.             document.write("$arr[i]");  //converted to a string constant
  15.             // how can I access my perl @arr variable?
  16.         }
  17.     </script>
  18.   </body>
  19. </html>
  20.  

I have also tried converting my array in perl to an object using the {} curley braces and the following:
Expand|Select|Wrap|Line Numbers
  1. my $js = JSON::objToJson({@arr});
  2. print $js
but how in javascript? I really appreciate any help. This is for a webserver I am working to build in my company.

Thanks,
Bobby
Nov 1 '07 #1
1 3621
bpejman
6 New Member
Ok, so after some more research, I think I figured out the JSON part. Hope this helps someone out there and this may not be the right or best way of doing it so please let me know if you have a better way.

So, as I push data into my array, I can take this object and pass it to JSON using the objToJSON function like so:

Expand|Select|Wrap|Line Numbers
  1. my @arr = ();
  2. for (my $i=0; $i<2; $i++) {
  3.     push @arr, $i;           # just so we can have integer indices (horrible)
  4.     push @arr, "value$i";
  5. }
  6.  
  7. my $js = objToJson({@arr});
Now, in my javascript, I can do the following:

Expand|Select|Wrap|Line Numbers
  1. var arr = $js;
BUT, there's a small problem. The arr's indices are of string kind where the 0th index is "value1" and the value associated to "value1" would be "value2"

In order for me to traverse through this, I MUST know the hash values, i.e.:

Expand|Select|Wrap|Line Numbers
  1. document.write(arr[0])  // will print "value1"
Does anyone know how I can use integer indices here so all I have to do in my javascript would be arr[0] without having to push the indices into the array? I would like for it to be an array for now and not a hash.

Thanks so much.
Nov 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
4901
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
4
2753
by: andrei.csibi | last post by:
I've have a .NET Assembly, which is COM Visible. I would like to load objects from this assembly in javascript code. E.g. Assembly Book.dll has the class Book and I would like to use <script language="javascript" type="text/javascript"> var book = new Book(); document.write(book.Title); </script> Even more, if I could access book through the document object it would be even more helpful, like: <script language="javascript"...
5
7846
by: turnitup | last post by:
Dear all, I have a JavaScript function that talks to php using ajax. It can return values from php. Up until now, I have used it to return simple values such as integers or text strings. However, I wish now to return an array. Is it possible to create an array in php, format it in some way and pass it back to javascript for use?
4
4804
by: googleAcct | last post by:
see the following example. When I simplified the test case, I seem to be getting an error I cant explain, though the read succeeds and I go on to iterate on the JSON result. Using Firebug or other mens, one can verify that the object was populated, and that the array is present. But I can't seem to get the expression to access it right. OR there is some kind of exception that is being silently thrown that I am not reading. Happy to put in...
2
12404
by: Garg | last post by:
Hi All, I am facing one problem if you are having any solution please tell me. I have to pass an array from javascript to servlet. for this i created one array and pass that through submitting the form with post method and i am using request.getParameterValues to get that array. But i am getting values in the first position of that array and that also comma separated so that also of no use for me.
11
26639
by: cybervigilante | last post by:
I can't seem to change the include path on my local winmachine no matter what I do. It comes up as includ_path .;C:\php5\pear in phpinfo() but there is no such file. I installed the WAMP package and PEAR is in c:\wamp\php\pear I modified php.ini in the c:\wamp\php directory to reflect the actual path, but even stopping and restarting my server shows the c: \php5\pear path. I can't change it no matter what I do I also tried the...
3
2304
by: xhe | last post by:
I found Jason is a very handy data format for client/server communication. But I just met a problem as follows: I want to read the data replied from server in Jason format, the reply is like this: it is generated automatically by amfphp1.9 from an array. I used Ajax to call a method, and the server code replied an array in the above
35
315
by: Dan Rumney | last post by:
Hi all, I've been writing Javascript for quite a while now and have, of late, been writing quite a lot of AJAX and AJAX-related code. In the main, my dynamically generated pages are created using Perl on the backend, with Javascript providing limited frontend functionality. As an example, an expanding tree would be fully populated on the server-side and then presented to the browser, with Javascript and CSS being used to vary the...
3
3918
by: you | last post by:
Hi there, File manupulation is difficult in Perl. I'd like to modify a file with JSON format. For example, test.conf is: { "tests" : , ... (and so on) }
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.