![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Member
Join Date: Oct 2002
Posts: 1,561
|
Hey all,
I just finished the first question of one of my perl assignments and I am so happy, I just had to share it with you all. Also, I wanted to ask if this was good programming style/habit or not?? so yeah, its in perl, it takes a user input of a string of words, then counts how many words in the string: (took out all comments) Code:
#!/usr/bin/perl -w
use strict;
my $text;
my @words;
my $count;
print("\n");
print("Please enter a string of text: ");
$text = <STDIN>;
chomp($text);
@words = split(/ /,$text);
$#words = $#words + 1;
print("The number of words in that string is: $#words \n\n");
Code:
Please enter a string of text: hello this is my first perl proggie, yay! The number of words in that string is: 8
__________________
"so i sat in my chair from 11pm till about 7am, scared shitless that some ghost chick was going to jump through my window and kill me. didn't happen though, so it's all good." ... |
|
|
|
| Join OCAU to remove this ad! |
|
|
#2 |
|
Member
Join Date: Oct 2002
Posts: 1,561
|
only problem i've realised is that two or more white spaces count as a word.... but i think thats ok as the lecturer says this:
"For simplicity lets just call a series of non-white space characters separated by white space (or newlines) a word." Thats what he means right?? Edit: hmm, maybe not....
__________________
"so i sat in my chair from 11pm till about 7am, scared shitless that some ghost chick was going to jump through my window and kill me. didn't happen though, so it's all good." ... Last edited by ludeking; 17th March 2004 at 12:00 AM. |
|
|
|
|
|
#3 |
|
Member
Join Date: Mar 2003
Location: Perth, Joondalup
Posts: 1,679
|
heheh
good stuff i just started learning Java so i know how you feel
__________________
I dont wanna be your designated driver when it's much more fun to be buzzing in the corner, YEAH! |
|
|
|
|
|
#4 |
|
Member
Join Date: Oct 2002
Posts: 1,561
|
Yeah I'm learning Java as well. One of my other courses!
__________________
"so i sat in my chair from 11pm till about 7am, scared shitless that some ghost chick was going to jump through my window and kill me. didn't happen though, so it's all good." ... |
|
|
|
|
|
#5 |
|
Member
Join Date: Oct 2002
Posts: 1,561
|
Anyone else got any feedback for me?
__________________
"so i sat in my chair from 11pm till about 7am, scared shitless that some ghost chick was going to jump through my window and kill me. didn't happen though, so it's all good." ... |
|
|
|
|
|
#6 |
|
Member
Join Date: Dec 2001
Location: Sydney
Posts: 181
|
>Also, I wanted to ask if this was good programming style/habit or not??
A few points, using "-w" and "use strict" is a great habit to get into, but you then throw away some of the benefits by declaring all your variables at the top of the program, effectively globalising them. Its considered good practice to scope your variables as tightly as possible, ie declare them just before you use them... (you're also declaring $count but never using it, you _sure_ thats not thowing a warning? ;-) The brackets are unneccessary with print and chomp statements (though the do work OK with them). Splitting on a literal space is almost certainly not what you _really_ want to do - that "//" in the split statement really is a regex pattern, so you can use \w to mean "any whitespace character", and \w+ to mean "one or more whitespade characters in a row" which is much more likely to "count words" accurately. Incrementing $#words is actually extending the @words array in memory, not a problem for a simple program like this, but adding 1 to the value when printing (or printing scalar(@words) instead) is argueably "better style". So, I get: Code:
#!/usr/bin/perl -w use strict; print"\nPlease enter a string of text: "; my $text = <STDIN>; chomp $text; my @words = split(/\w+/,$text); print "The number of words in that string is: ".$#words+1."\n\n"; ;-) big |
|
|
|
|
|
#7 |
|
Member
Join Date: Mar 2003
Posts: 43
|
ludeking, what course are you doing?
|
|
|
|
|
|
#8 |
|
Member
Join Date: Oct 2002
Posts: 1,561
|
hey guys,
The lecturer told us we need to use the -w flag and the use strict as well so thats why they are there. Yeah I forgot to delete the $count... whoops.... I'm doing a Masters Degree in InfoTech edw. Thanks for the revised code, I'll look it over!
__________________
"so i sat in my chair from 11pm till about 7am, scared shitless that some ghost chick was going to jump through my window and kill me. didn't happen though, so it's all good." ... |
|
|
|
|
|
#9 |
|
Member
Join Date: Jun 2001
Location: Seattle
Posts: 1,162
|
Now you need to star learning obfusicated perl which, if I haven't forgotten much is this:
Code:
#!/usr/bin/perl -w use strict; print"\nPlease enter a string of text: "; <>; chomp; print "The number of words in that string is:" + scalar(split(/\w+/,) + "\n\n"; or even: Code:
#!/usr/bin/perl -w use strict; print"\nPlease enter a string of text: "; print "The number of words in that string is:" + scalar(split(/\w+/,chomp(<>)) + "\n\n"; edit: debugging edit2: the board sticks a space between the > and the ) for some reason :P. Last edited by Shalmanese; 17th March 2004 at 7:15 PM. |
|
|
|
|
|
#10 |
|
Member
Join Date: Oct 2002
Posts: 1,561
|
I cant get any of your code to compile and run however I thunk I can see what you are getting at.
__________________
"so i sat in my chair from 11pm till about 7am, scared shitless that some ghost chick was going to jump through my window and kill me. didn't happen though, so it's all good." ... |
|
|
|
|
|
#11 |
|
Member
Join Date: Oct 2002
Posts: 1,561
|
Argh,
I tried incorporating this: Code:
@words = split(/\w+/,$text); Oh well, I'll move on and come back to it. Thanks for all your ideas anyway!!
__________________
"so i sat in my chair from 11pm till about 7am, scared shitless that some ghost chick was going to jump through my window and kill me. didn't happen though, so it's all good." ... |
|
|
|
|
|
#12 | |
|
Member
Join Date: Jun 2001
Location: Seattle
Posts: 1,162
|
Quote:
for example: Code:
while(<>) { print;}
Code:
print while (<>); )For the second one, chomp can't work on handles since it tries to modify the string directly rather than copy it into another location. A simple but dodgy hack would be to omit the chomp but to minus 1 from the word count. the debugged code is this: Code:
#!/usr/bin/perl print"\nPlease enter a string of text: "; print "The number of words in that string is: ", scalar(split(/\w+/,<>)) - 1, "\n\n"; Code:
print "The number of words in that string is: ", scalar(split(/\w+/,<>)) - 1, "\n\n" if print"\nPlease enter a string of text: ";; BUGS The -w switch is not mandatory. ROTFL! |
|
|
|
|
|
|
#13 | |
|
Member
Join Date: Jun 2001
Location: Seattle
Posts: 1,162
|
Quote:
stuff like: Code:
sdfsdfsdf ???!?!?! sddsff ^EOL |
|
|
|
|
|
|
#14 | |
|
Member
Join Date: Oct 2002
Posts: 1,561
|
Quote:
I found the correct argument to split on. Code:
split(/\s+/$variable) Final code: Code:
#!/usr/bin/perl -w
use strict;
my $text;
my @words;
print("\n");
print("Please enter a string of text: ");
$text = <STDIN>;
$text=~s/^\s+//;
variable
chomp($text);
@words = split(/\s+/,$text);
$#words = $#words + 1;
print("The number of words in that string is: $#words \n\n");
Code:
Please enter a string of text: this is a test and it works! The number of words in that string is: 7 p.s. I just modified it to read lines till EOF and report number of words for each line of input!!
__________________
"so i sat in my chair from 11pm till about 7am, scared shitless that some ghost chick was going to jump through my window and kill me. didn't happen though, so it's all good." ... Last edited by ludeking; 17th March 2004 at 9:23 PM. |
|
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|