blueberry has asked for the wisdom of the Perl Monks concerning the following question:
hello there,
i have this script it needs correction,so that it correctly runs#
#The first script conjugates a verb in present simple#
@pronouns_ending=("i","you","he/she/it","we","you","they","","","","s"
+,"","");
print"Give a verb to be conjugated in present simple :\n\nVerb:\t";
$verb=<<>>;
split $verb;
print "\n";
for($i++;$i<4;){
print pop(@pronouns_endings)." ".$verb-3.shift(@pronouns_endings).
+"\n";
Re: perl experts .. plz
by davido (Cardinal) on Mar 21, 2016 at 15:05 UTC
|
- The input captured into $verb will probably end in a newline. You should use chomp to rid your string of that unwanted clutter.
- Your split statement splits into void context (the return value is not captured), and the parameters are wrong; you certainly don't want to treat $verb as a pattern.
- Your for statement increments $i before entering the loop, and never again. The $i++ should appear after the last semicolon, not before the first one.
- Your second print statement includes $verb-3, which will result in -3 being printed, and if warnings are enabled, a warning being generated, because $verb will probably not contain anything resembling a number, so Perl will treat it as a zero in numeric context.
- You are using @pronouns_ending to hold two types of data in a confusing way. Better to use an array of array-refs, or some data structure that better reflects the relationships of the data that are currently only represented by partitioning the array.
- You lack a final }, preventing compilation.
- You aren't using strict and warnings -- tools that will aid in preventing or discovering other common pitfalls.
| [reply] [d/l] [select] |
|
thank you Mr. davido, but again i don't get it .. it's supposed to be an easy question i know, but honestly am very week in perl.. would u plz help me correct and creat a script out of this:( using loop for)
#.............................#
@pronouns_ending=("i","you","he/she/it","we","you","they","","","","s","","");
print"Give a verb to be conjugated in present simple :\n\nVerb:\t";
$verb=<<>>;
split $verb;
print "\n";
for($i++;$i<4;){
print pop(@pronouns_endings)." ".$verb-3.shift(@pronouns_endings)."\n";
}
| [reply] |
|
Mr. Dave
i tried to follow your instructions and that's what i got
#..................#
use strict;
use warning;
@pronouns_ending=("i","you","he/she/it","you","they","","","s","","");
print"remember :\n\nVerb:\t";
chomp $verb=<<>>;
split $verb;
print "\n";
for($i++;$i<4;){
print pop(@pronouns_endings($i))." ".$verb-3.shift(@pronouns_endings(i+5))."\n";
}
| [reply] |
Re: perl experts .. plz
by GotToBTru (Prior) on Mar 21, 2016 at 11:54 UTC
|
Help us help you!
- You should make the title of your post about your post.
- Please put code tags around your code. It is unreadable.
As written, it will not compile. You're missing the closing parentheses for your for loop. You use split but don't store the result. You subtract 3 from a string; that makes no sense.
But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)
| [reply] [d/l] [select] |
|
hello again
I do apologize .. actually i am a beginner in perl and this is a homework. our teacher asked us to rewrite this kind of a puzzle.. he wants us to discover where the mistakes are.. so .. would you please help me on this coz am lost and frustrated
| [reply] |
|
blueberry: It's good that you've frankly stated that your OP is a homework question, something that I'm sure many of us suspected. However, please realize that the Monastery is not a homework completion service.
I and many others will be glad to provide guidance, and GotToBTru and davido have already given you some very valuable advice and hints. Please see CountZero's reply if you want to see some code with Adult Content (just for reference of course). Also, please consult your course materials.
Again, please be careful to post code in <code> ... </code> tags as has been suggested. Your OP has been cleaned up (whether by you or someone else I know not), but other replies of yours are still almost unreadable, and the Monks get very grumpy if they must do a lot of work in order to provide free advice!
Give a man a fish: <%-{-{-{-<
| [reply] [d/l] [select] |
|
|
Re: perl experts .. plz
by CountZero (Bishop) on Mar 21, 2016 at 19:50 UTC
|
Further to the comments already given, your program is a very naive implementation of a conjugation script. Of course, it will give the wrong answer for lots of more or less irregular verbs. So even when you correct all the syntax errors, it will still not run "correctly" in the sense that it will not give you the right answers.And yet, no need to despair or re-invent the wheel when CPAN will provide you with such nice modules as Lingua::EN::Conjugate: use Modern::Perl qw/2015/;
use Lingua::EN::Conjugate qw/conjugate/;
while ( my $verb = <DATA> ) {
chomp $verb;
for my $pronoun (qw/I you he we you they/) {
say conjugate(
verb => $verb,
pronoun => $pronoun,
allow_contractions => 1,
tense => 'present',
);
}
say '---------------------';
}
__DATA__
walk
hear
be
have
go
try
sing
may
Output:I walk
you walk
he walks
we walk
you walk
they walk
---------------------
I hear
you hear
he hears
we hear
you hear
they hear
---------------------
I'm
you're
he's
we're
you're
they're
---------------------
I've
you've
he's
we've
you've
they've
---------------------
I go
you go
he goes
we go
you go
they go
---------------------
I try
you try
he tries
we try
you try
they try
---------------------
I sing
you sing
he sings
we sing
you sing
they sing
---------------------
I may
you may
he may
we may
you may
they may
---------------------
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] [d/l] [select] |
|
Sounds great .. but am afraid my teacher won't allow working on another version of the program. i don't know.. is there any other possible way to run it correctly on my current version.. and one more thing i forgot to mention in his question he wrote:" The first script conjugates a verb in present simple".. not in the past
| [reply] |
|
If you could explain, line by line, what your rationale is for the code you wrote, then perhaps we could be of further assistance in facilitating your learning process. I could write the thing for you in a couple of minutes. I've already devoted more time than that to trying to guide you in the right direction instead -- I feel no compelling need to write five lines of code that you can turn in to your teacher. My motivation is to help a newcomer get the sort of start with Perl that he or she will be able to build upon, and just writing some code for you, though it would save you and I both time this time around, won't contribute toward you becoming proficient with Perl, won't prepare you for the next assignment, and won't start someone on the path toward becoming a Perl user as time goes by.
By you going through the process of explaining line by line why you made the choices you did in writing the code, we can better understand your thought-process, and we will be able to provide you useful insights. In particular, I'd like to know if it is dictated by the assignment itself that you use split, shift, pop, and a C-style for loop. If not, those are odd choices. Was it specified in the assignment that you use a single array to represent both the tenses and the suffixes? Again, a strange choice. But we really don't know which of your design choices are required elements in the assignment, and which are all yours. ...and if all yours, why.
The most immediate problem is that you left off a closing curly brace. Curly-braces are almost always supposed to match, a left needs a corresponding right. Same with parenthesis and square brackets. If you add that to the end of your script where it belongs, the script should compile but still won't work, for some of the other reasons I enumerated in an earlier post.
Have you read perlintro? You've spent more time fiddling with code and posting here than it takes to read perlintro, and it contains about 90% of what you would need to complete your assignment. After doing that, think it through; think to yourself, "How would I solve this with pencil and paper?" Your code doesn't demonstrate that thought process. Instead, it seems more like you pulled elements out of your lecture notes and inserted them into the code without understanding what they do. Once you have a concept of how it might be solved with pencil and paper, start writing code that does that, and add nothing that doesn't move you toward that goal... well nothing aside from strict and warnings.
| [reply] [d/l] [select] |
|
|
The script I wrote conjugates the verbs in the present tense. What makes you think it conjugates the verbs in the past tense? It would be trivially easy to make it conjugate the verbs in the past tense (just change "present" to "past" in my program) but that is not the issue.The issue is - in my opinion - that even if you make your program run, it will still not run correctly, as it does not take into account how to deal with irregular verbs or certain well-known grammatical exceptions. If you wish to take care of that, then you will end up with a program very similar to mine.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] |
|
|
|
| [reply] |
|
Re: perl experts .. plz
by AnomalousMonk (Archbishop) on Mar 21, 2016 at 14:32 UTC
|
| [reply] [d/l] |
Re: perl experts .. plz
by FreeBeerReekingMonk (Deacon) on Mar 25, 2016 at 23:06 UTC
|
Blueberry, can you update your first post and tell us which errors you have found and fixed so far? If you fix a few I'll give you more pointers.
Did you already found out what you need to change that double diamond operator to read from STDIN?
Sorry we make you sweat a little... we want you to learn in the process... and that takes some effort. Maybe team up with another classmate?
TIP: Go line by line. Write only the first line, run the perl script. If it runs, add another line, run again... | [reply] |
|
|