vik has asked for the wisdom of the Perl Monks concerning the following question:

i think i found a solution,

#use strict; #use warnings; open (FILE, 'german.txt'); @words = <FILE>; $num = int rand(@words); ($article, $noun) = split(/ /, $words[$num]); chomp($noun); chomp($article); print "Who is the article of $noun:"; chomp($resp = <>); if ($resp eq $article) { print "Correct!"; } else { print "The correct answer is $article!" }

Can you please tell me why the program is not running with use strict and use warnings?

Edit: g0n - Original content (removed by OP):

Hello monks, i am learning german and i want your help. I have several nouns with their article in one text file, one article and noun by row, ex das Zimmer (next line) die Brille (next line) der Reisebus. Please help me to write a program who reads the text file and randomly ask me for the article of a noun, and tell me if my answer was correct or false and the correct answer. Thanks you.

Edit: g0n - Code & formatting tags

Replies are listed 'Best First'.
Re: german nouns gender
by imp (Priest) on Nov 08, 2006 at 16:37 UTC
    What have you tried so far? Monks tend to offer more help to people who are trying to solve a problem, versus people looking to have the community write their code for them.

    Good places to start would be open and split.

    The basic flow of the program should be something like this:

    1. Open the file
    2. Read each line, splitting the text into article and noun.
    3. Find the total number of entries
    4. Generate a random number between 1 and the last index of the set
    5. Loop, Printing the noun and testing user input against the article
    A few hints:
    Open a file:
    open my $fh, '<', $filename or die "Failed to open $filename - $!";
    Read the file one line at a time:
    while (my $line = <$fh>) { # Do something with $line }
    Check the number of items in a list (by evaluating a list in scalar context):
    my $count = @list;
    Get a line of input:
    my $answer = <STDIN>;
    Update - hint: always use strict and warnings
    Always use the following at the top of your script:
    use strict; use warnings;
    By default perl is extremely forgiving and tries to do what you mean. Sometimes this is desireable, but normally you want perl to be strict and catch the mistakes you make. Save yourself a lot of debugging time by always using both strict and warnings.
Re: german nouns gender
by ww (Archbishop) on Nov 08, 2006 at 16:39 UTC
    vik
    At just about the time you were posting this, there was a discussion in the CB of a proper question for the Monks.

    In that particular case, it boiled down to "we try to teach, but we're not generally inclined to simply write a program for those who are SOPW (or looking for a free handout)." In a manner of speaking, ou even burn a candle to that practice, when you ask "Please help me to write a program..."

    But I suspect that was offhand; and merely a form of speaking that you see as a "polite" formulation of the plea you seem to be making: "please write me a program...."

    So, please show us the answers to the questions "What's your code look like? What have you tried? What sort of failure/error results?"

Re: german nouns gender
by xorl (Deacon) on Nov 08, 2006 at 16:33 UTC
    I'd do something like, open the file and run through it. For each line I'd print out the noun and wait for a responce from STDIN. Then check the responce with the answer and print a the result. It shouldn't be overly difficult.
Re: german nouns gender
by shmem (Chancellor) on Nov 08, 2006 at 16:46 UTC
    perl -e '@a{qw(der die das)}=(1..3);open(I,shift);chop(@l=<I>);{@s=spl +it/\s+/,$l[rand(@l)];print "$s[1](1=der,2=die,3=das)? ";chop($w=<STDI +N>);print $w==$a{$s[0]}?"yes\n":"no: @s\n"; redo}' german.txt

    update: While dirty hack, 'tis presented to the OP as an incentive to grok how it works, and to learn.

    --shmem

    500th post

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: german nouns gender
by smokemachine (Hermit) on Nov 08, 2006 at 16:37 UTC
    coud be this?
    $|=1; @data = <DATA>; while($resp ne "q"){ $num=int rand(@data); ($a,$b)=split/ /,$data[$num]; print "$b: "; chomp($resp = <>); print $a eq lc $resp?"Rigth\n":"Wrong\n" } __DATA__ das Zimmer die Brille der Reisebus
      ($a,$b)=split/ /,$data[$num];

      It's generally a bad idea to use $a and $b a general variables in Perl programs. They are special variables that are used in sort routines and that's the only place where they should be used.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

      Not quite. If the first answer is wrong, this code does NOT give the correct answer, as OP requested, but gets stuck in a loop.

      C:\perl germ.pl Reisebus: das Wrong Reisebus: den Wrong Reisebus: the Wrong Reisebus: der Rigth Reisebus: Wrong Reisebus: Terminating on signal SIGINT(2)

      No downvote for that; just a note; but please see the tenor of xorl 's reply and the very explicit admonitions from others, (including /me, below)... and consider the "fish" story -- " Teach a man to fish...."

      When offering instruction to someone who is obviously new to perl it would be best to take the opportunity to teach them. Whether that is done by providing hints, or providing a solid solution is of course up to the author.

      The solution you provided is close to what he was asking for, but it would have been better to use strict and warnings to encourage good habits, and it would be good to offer links to the syntax/idioms that might be unfamiliar to the original poster. For example, he might not be familiar with the ternary operator or the autoflush variable

A reply falls below the community's threshold of quality. You may see it by logging in.