0

Here is a similar example of what I am trying to do:

@name = qw (Sam Tom John Mike Andrea);
@scores = qw (92 80 59 83 88);

I need to store these array as JavaScript so I can make useful graphs on the web page.

1

1 Answer 1

2

Assuming you want to keep them as separate arrays, stick references to them in a hash first:

my %data = ( names => \@names, scores => \@scores );

Then use the JSON module to serialize the data structure to JSON, e.g.:

use strict;
use warnings;

use JSON;

my @names  = qw (Sam Tom John Mike Andrea);
my @scores = qw (92 80 59 83 88);

my %data = ( names => \@names, scores => \@scores );

my $json = encode_json \%data;

print $json

Output:

{"names":["Sam","Tom","John","Mike","Andrea"],"scores":["92","80","59","83","88"]}
Sign up to request clarification or add additional context in comments.

2 Comments

This makes sense but here is what I have.
Then I suggest asking a new question about what you are actually trying to do, and what you are having trouble with. If you just make up an irrelevant question, the answer probably won't help you.

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.