0

I have an array of unknown size in perl generated by some other perl module. Now, precisely I want to find if a value passed to a jquery function exists in the perl array or not.

Is there a way I can do an element by element comparison of the input value against each value in perl array?

I looked around and looks like I can access perl array in jquery by providing the index but we don't know the size of the array. So I don't know when to stop.

My mason code looks something similar to:

<%perl>
    my @testArray = [call to some other perl module to get the values]
</%perl>

<script type="text/javascript">
    function checkIfValExistsInTestArray(val) {
        // Code to test if "val" exists in "@testArray". Returns boolean true/false.
    }
</script>
6
  • Can you share with us what you have tried? Commented Jul 17, 2014 at 16:34
  • since they both run in different environments and at different times you need to be far more specific Commented Jul 17, 2014 at 16:37
  • @charlietfl I edited the statement and added the sample scenario how my code looks like. I'm fairly new to this technology hence half cooked queries. Commented Jul 17, 2014 at 16:55
  • @JayBlanchard I followed following example: forums.devshed.com/perl-programming-6/… but since this wouldn't work for unknown size array, I was lost. Remaining all what I tried before reaching this approach doesn't suit this context hence not describing. Commented Jul 17, 2014 at 16:58
  • 1
    unless you plan to output the whole array as javascript variable you could make ajax call to validate the variable Commented Jul 17, 2014 at 17:01

1 Answer 1

2

To check for existence, you'd want a hash. A simple way of transmitting the data would be to encode it using JSON.

% use JSON qw( );

<script type="text/javascript">

var testArray = <% JSON->new()->encode({ map { $_ => 1 } get_values() }) %>;

function checkIfValExistsInTestArray(val) {
   return testArray[val];
}

</script>

For example, if get_values() returned apple and orange, you'd get

<script type="text/javascript">

var testArray = {"apple":1,"orange":1};

function checkIfValExistsInTestArray(val) {
   return testArray[val];
}

</script>

I don't know Mason, so there could be errors, but you get the idea.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for quick reply. Thanks a ton. Looking at your edited version it makes clear to me what we are trying to do here. You are serializing the perl versioned list of array to JSON map with values having 1 and trying to lookup into the map. I have no idea of the JSON modules being used but this is what I could pull out from your code. Please correct me if 'm wrong.
That's right. JSON is a subset of JS, so you are basically generating JS code to create an Object you can query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.