in reply to copying perl array to javascript array

You have to convert the array to a string in Perl, then back to an array in JScript. A while ago, I wrote a serializer I can adapt to this situation. Here it is:

In Perl, change

sub populate { my @array = (...); return @array; }

to

# Serializes an array, a hash or a list which contains only # strings and undefs. Everything else will be stringified. sub serialize_string_list { return join('|', map { (defined($_) ? do { local $_=$_; s/\^/^1/g; s/\|/^2/g; $_ } : '^0' ) } @_ ); } sub populate { my @array = (...); return serialize_string_list @array; }

In the emitted JavaScript, change

function do_populate_cb(s) { ... do something with s... }

to

// Deserializes a list serialized with serialize_string_list. function deserialize_string_list(s) { var fields = s.split("|") var i; for (i=0; i<fields.length; i++) { if (fields[i] == "^0") { fields[i] = null; } else { var re; re = /\^2/g; fields[i] = fields[i].replace(re, "|"); re = /\^1/g; fields[i] = fields[i].replace(re, "^"); } } return fields; } function do_populate_cb(s) { var array = deserialize_string_list(s); var i; for (i=0; i<array.length; i++) { ... do something with array[i]... } }

Important: The above only supports lists (and arrays) of strings. Lists of numbers will work, but they will be converted to arrays of strings. List containing references of any sort will not work as expected.

Background: I didn't use a single-character escape mechanism (such as preceeding metacharacters with a slash) since it makes deserialization hard. (i.e. "Should I split on this pipe, or is that an escaped pipe?") The escape mechanism I used -- replacing the seperator character with another character -- avoids that problem, simplifying parsing. IP over Serial Line (SLIP) and maybe Point to Point Protocol (PPP) use a similar escaping algorithm to escape packet delimiters because the delimiters cannot appear inside a packet.

Replies are listed 'Best First'.
Re^2: copying perl array to javascript array
by Rainmaker (Acolyte) on Aug 02, 2005 at 18:57 UTC
    Greetings ikegami
    I tried using your code. Its works fine except for the fact that the last array value is followed by the whole html code in the form.
    For eg:- If the array values are A,B,C,D and i add them in the drop down menu then the last option(D) is followed by the whole html code. So I have four options in the drop down box A,B,C,D<htmlcode>
    Am i doing something wrong?
    See code below.
    sub serialize_string_list { return join('|', map { (defined($_) ? do { local $_=$_; s/\^/^1/g; s/\|/^2/g; $_ } : '^0' ) } @_ ); } sub populate{ @n=(); my $dt; my $row; #course contains the values fetched from the database foreach $row (@$course) { ($dt) = @$row; push(@n,$dt); } return serialize_string_list @n; }; //deserializes the array function deserialize_string_list(s) { var fields = s.split("|") var i; for (i=0; i<fields.length; i++) { if (fields[i] == "^0") { fields[i] = null; } else { var re; re = /\^2/g; fields[i] = fields[i].replace(re, "|"); re = /\^1/g; fields[i] = fields[i].replace(re, "^"); } } return fields; } //calling Sajax wrapper function from a javascript function function do_populate() { x_populate(do_populate_cb); } function do_populate_cb(s) { var arr = deserialize_string_list(s); var i; for (i=0; i<arr.length; i++) { with(document.doc1.selCourseNm) { options[i]=new Option(arr[i],arr[i]); } } }

      Did you forget to close your SELECT tag?

      How about you show us runnable code that exhibits the problem.

      By the way. I can't find any documentation for Option's contructor. In fact, according to the documentation, nodes should be created using createElement and createTextNode methods. What follows uses the correct methods.

      function do_populate_cb(s) { var arr = deserialize_string_list(s); var options = document.doc1.selCourseNm.options; var i; // Remove any options it currently has. options.length = 0; // Add new options. for (i=0; i<arr.length; i++) { var option = document.createElement('OPTION'); option.value = arr[i]; option.innerText = arr[i]; options.add(option); } }
        Greetings Monks, Sorry for the late response. I was away so cudnt post earlier.
        I'm still having the same old problem in which the html code gets appended to the drop down options. You can see the live form at http://tigger.uic.edu/htbin/survey-send.pl
        Try selecting an option from the CourseDept dropdown and the CourseName combobox will be dynamically populated. Check out the last option in the Coursename drop down menu. It has the html course appended to it. Also when you change the CourseDept option the CourseName combo box shows some of the old options aswell. This was not happening before. It started happening only after i started using Sajax.
        I'm new to web stuff esp. perl and using perl javascript together is giving me problems.
        Can you please help?
        Thanks