Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: How could I create a command line quiz?

by yasser (Initiate)
on Mar 12, 2020 at 15:25 UTC ( #11114177=note: print w/replies, xml ) Need Help??

in reply to How could I create a command line quiz?

I feel like I'm getting warmer and that I have to move the foreach loop into the global section. Thanks for the advice thus far. I'm reading the docs prescribed by you all.

#!/usr/bin/perl print "Let's start! \n"; print question(); chomp($input=<STDIN>); print answer(); sub question { @array_q = `cat quiz.txt | grep ?`; foreach(@array_q){print $array_q[0] } } sub answer { $/ = -00; @array_a = `cat quiz.txt | grep -v ?`; foreach(@array_a){print $array_a[0] } } bash-3.2$ ./ Let's start! Name 2 shapes? Name 2 shapes? Name 2 shapes? sadf square circle North America Neil Armstrong bash-3.2$

Replies are listed 'Best First'.
Re^2: How could I create a command line quiz?
by Tux (Canon) on Mar 12, 2020 at 15:55 UTC

    Did you *really* read all the answers and advice? If you did, why didn't you follow them or what was unclear?

    You still use backticks and external commands to do what perl itself is perfectly capable of doing (and people told you).

    Enjoy, Have FUN! H.Merijn

      Yes, you're right. I want to get the structure of the script correct first and then I'll replace the backticks with perl code

      I've built up to this part slowly by testing a basic subroutine that outputs static values, then added more functionality as my knowledge builds.

        the structure of the script
        Hmm. You seem to do
        1. read the list of questions in
        2. print the first question
        3. read the list of answers
        4. print the first answer
        Now, how would you go for a random question, considering that there can be multiple answers for a question?
        1. read the list of questions in - ok
        2. print a random question - ok
        3. read the list of answers - ok
        4. print … which answer???
        Better control flow would be:
        1. read the list of questionss and answers, preserving the relation between q and a
        2. select a random q&a group
        3. print the selected question
        4. print the corresponding answers
        Problem: you need a structure, yes, but not only of the script (control flow) but also for your data (be that arrays or objects). That might be possible with bash and grep, but that will be very specific and therefore difficult to translate to any other language, including Perl

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11114177]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2022-05-25 20:17 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (90 votes). Check out past polls.