CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Data Structure Loop

 



ogit2
Novice

Oct 17, 2017, 8:48 AM

Post #1 of 4 (695 views)
Data Structure Loop Can't Post

Hi

I have the following data structure (received from 3rd party web site so I cannot change format) and am trying to find a way to output status and direction for each question (questions) using a loop. I can access them individually. Hopefully a simple query.


Code
   my $ans =  $data->{questions}->{textQuestion}->{status}; 
my $quest = $data->{questions}->{textQuestion}->{direction};



Code
$VAR1 = { 
'questions' => {
'textQuestion' => {
'status' => 'incorrect',
'direction' => "How many types of features does an owl have?",
},
'numericQuestion' => {
'status' => 'correct',
'direction' => "The interior angles of a triangle add up to? ",
},
'textQuestion' => {
'status' => 'correct',
'direction' => "Please spell the opposite of majestic ",
},
'multipleQuestion' => {
'status' => 'incorrect',
'direction' => "Which of these can be found in a swamp ",
},
'selectionQuestion' => {
'status' => 'correct',
'direction' => "Select the correct wood for building firewood ",
}
},
'groups' => {
'group' => {
'passingPercent' => '80',
'name' => 'Question Group 1',
'passingScore' => '80',
'totalQuestions' => '10',
'awardedScore' => '80.83',
'answeredQuestions' => '7',
'awardedPercent' => '80.83'
}
},
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'quizSettings' => {
'maxScore' => '100',
'maxNormalizedScore' => '100',
'timeLimit' => '1200',
'passingPercent' => '0.8',
'quizType' => 'graded'
},
'xmlns' => 'http://results',
'version' => '1'
};



(This post was edited by ogit2 on Oct 17, 2017, 8:57 AM)


BillKSmith
Veteran

Oct 17, 2017, 10:15 AM

Post #2 of 4 (685 views)
Re: [ogit2] Data Structure Loop [In reply to] Can't Post

Your data structure does not compile. Based on the indenting, I made a guess at what is missing (Refer to comment in my code). If this is not correct, the rest of my code probably will not help you.

Note: It is not possible to preserve the original order of the questions.


Code
C:\Users\Bill\forums\guru>type ogit2.pl 
use strict;
use warnings;

my $data = {
'questions' => {
'textQuestion' => {
'status' => 'incorrect',
'direction' => "How many types of features does an owl have?",
},
'numericQuestion' => {
'status' => 'correct',
'direction' => "The interior angles of a triangle add up to? ",
},
'textQuestion' => {
'status' => 'correct',
'direction' => "Please spell the opposite of majestic ",
},
'multipleQuestion' => {
'status' => 'incorrect',
'direction' => "Which of these can be found in a swamp ",
},
'selectionQuestion' => {
'status' => 'correct',
'direction' => "Select the correct wood for building firewood ",
}
}, # I added this line to correct syntax
'groups' => {
'group' => {
'passingPercent' => '80',
'name' => 'Question Group 1',
'passingScore' => '80',
'totalQuestions' => '10',
'awardedScore' => '80.83',
'answeredQuestions' => '7',
'awardedPercent' => '80.83'
}
},
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'quizSettings' => {
'maxScore' => '100',
'maxNormalizedScore' => '100',
'timeLimit' => '1200',
'passingPercent' => '0.8',
'quizType' => 'graded'
},
'xmlns' => 'http://results',
'version' => '1'
};

while (my ( $question, $ans_ref ) = each %{$data->{questions}} ) {
print "$question:\n";
print "status = $ans_ref->{status}\n";
print "direction = $ans_ref->{direction}\n";
print "\n\n";
}

C:\Users\Bill\forums\guru>perl ogit2.pl
numericQuestion:
status = correct
direction = The interior angles of a triangle add up to?


textQuestion:
status = correct
direction = Please spell the opposite of majestic


selectionQuestion:
status = correct
direction = Select the correct wood for building firewood


multipleQuestion:
status = incorrect
direction = Which of these can be found in a swamp

Good Luck,
Bill


ogit2
Novice

Oct 19, 2017, 12:40 AM

Post #3 of 4 (674 views)
Re: [ogit2] Data Structure Loop [In reply to] Can't Post

Hi

Thank you Bill (and for the correction). Your solution worked perfectly. I am still getting my head around data structures. Not easy.


BillKSmith
Veteran

Oct 19, 2017, 10:16 AM

Post #4 of 4 (666 views)
Re: [ogit2] Data Structure Loop [In reply to] Can't Post

Perl's own documentation should be a big help. For help in using the document reader, at your command line, type:

Code
perldoc perldoc


Study the document perldsc (and all the documents referenced in its 'SEE ALSO' section).

Note that my code is a minor generalization of the first example in the documentation for the function 'each' (refer to the document perlfunc).

It would be helpful to others if you would edit your original post. Add the missing line and a comment indicating that it is a correction. Verify that you can download and compile the corrected code.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives