Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: german nouns gender

by imp (Priest)
on Nov 10, 2006 at 15:42 UTC ( #583349=note: print w/ replies, xml ) Need Help??


in reply to Re: german nouns gender
in thread german nouns gender

In the future please preview your posts, and read Writeup Formatting Tips

One of the main things enforced by strict is that you need to declare your variables, typically with my.

Here is the code you provided, in the <code> tags:

#use strict; #use warnings; open (FILE, 'german.txt'); @words = <FILE>; $num = int rand(@words); ($article, $noun) = split(/ /, $words[$num]); chomp($noun); print "Who is the article of $noun:"; chomp($resp = <>); if ($resp eq $article) { print "Correct!"; } else { print "The correct answer is $article!" }
A few problems:
  1. You need to declare the variables with my
  2. You should check that the file was opened successfully
Here is a corrected version
use strict; use warnings; open (FILE, 'german.txt') or die "Failed to open german.txt - $!"; my @words = <FILE>; my $num = int rand(@words); my ($article, $noun) = split(/ /, $words[$num]); chomp($noun); chomp($article); print "Who is the article of $noun:"; my $resp = <>; chomp $resp; if ($resp eq $article) { print "Correct!"; } else { print "The correct answer is $article!" }


Comment on Re^2: german nouns gender
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (17)
As of 2014-07-31 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (253 votes), past polls