I have an issue when I try to execute a loop to handle a json file, I am trying to replace the Zero in "correct":0 to the right answer number tagged by {C} for example for Question 1 I need to put "correct":1 and for Question 2 I need to put "correct":3 and so on ...
this is my perl file :
#!/usr/bin/perl
use strict;
use warnings;
binmode STDOUT, ":utf8";
use utf8;
use JSON;
my $json;
{
local $/; #Enable 'slurp' mode
open my $fh, "<", "test.json";
$json = <$fh>;
close $fh;
}
my $data = decode_json($json);
# Output to screen one of the values read
my $var = $data->{'question'}->{'answers'};
my ($correctans) = grep {s/.*(\d).*/$1/ if m/C/} (my @answers = qw($var));
# Modify the value, and write the output file as json
$data->{'question'}->{'correct'}->[0] = $correctans;
open my $fh, ">", "test.json";
print $fh encode_json($data);
close $fh;
my json file :
{
"introduction":"This quiz tests you about foo and goo",
"questions":[
{"question":"Why is the sky blue?",
"answers":["Unicorns{C}","Fairies","Boring Science","Kittens"],
"correct":0},
{"question":"Why are kittens so cute?",
"answers":["Magic","Fur","Meow{C}","More Kittens!"],
"correct":0}
]
}
Any help ?