Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 16th March 2004, 11:53 PM   #1
ludeking Thread Starter
Member
 
ludeking's Avatar
 
Join Date: Oct 2002
Posts: 1,561
Default my first real perl prog!

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");
output
Code:
Please enter a string of text: hello this is my first perl proggie, yay!
The number of words in that string is: 8
YAY!
__________________
"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."
...
ludeking is offline   Reply With Quote

Join OCAU to remove this ad!
Old 17th March 2004, 12:00 AM   #2
ludeking Thread Starter
Member
 
ludeking's Avatar
 
Join Date: Oct 2002
Posts: 1,561
Default

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.
ludeking is offline   Reply With Quote
Old 17th March 2004, 1:10 AM   #3
=xer0=
Member
 
=xer0='s Avatar
 
Join Date: Mar 2003
Location: Perth, Joondalup
Posts: 1,679
Default

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!
=xer0= is offline   Reply With Quote
Old 17th March 2004, 11:53 AM   #4
ludeking Thread Starter
Member
 
ludeking's Avatar
 
Join Date: Oct 2002
Posts: 1,561
Default

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."
...
ludeking is offline   Reply With Quote
Old 17th March 2004, 11:54 AM   #5
ludeking Thread Starter
Member
 
ludeking's Avatar
 
Join Date: Oct 2002
Posts: 1,561
Default

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."
...
ludeking is offline   Reply With Quote
Old 17th March 2004, 12:55 PM   #6
bigiain
Member
 
Join Date: Dec 2001
Location: Sydney
Posts: 181
Default

>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";
Having said that, yours works, and working code is much better than fabulously stylish code that doesn't compile to provide the right answer!

;-)

big
bigiain is offline   Reply With Quote
Old 17th March 2004, 4:09 PM   #7
edw
Member
 
Join Date: Mar 2003
Posts: 43
Default

ludeking, what course are you doing?
edw is offline   Reply With Quote
Old 17th March 2004, 7:03 PM   #8
ludeking Thread Starter
Member
 
ludeking's Avatar
 
Join Date: Oct 2002
Posts: 1,561
Default

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."
...
ludeking is offline   Reply With Quote
Old 17th March 2004, 7:12 PM   #9
Shalmanese
Member
 
Shalmanese's Avatar
 
Join Date: Jun 2001
Location: Seattle
Posts: 1,162
Default

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";
I think thats it.

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";
I
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.
Shalmanese is offline   Reply With Quote
Old 17th March 2004, 7:31 PM   #10
ludeking Thread Starter
Member
 
ludeking's Avatar
 
Join Date: Oct 2002
Posts: 1,561
Default

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."
...
ludeking is offline   Reply With Quote
Old 17th March 2004, 8:08 PM   #11
ludeking Thread Starter
Member
 
ludeking's Avatar
 
Join Date: Oct 2002
Posts: 1,561
Default

Argh,
I tried incorporating this:
Code:
@words = split(/\w+/,$text);
and I am still getting more than one white space being a word, plus chars like ? being a word, plus if there is only one word then $#words = 0!!!

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."
...
ludeking is offline   Reply With Quote
Old 17th March 2004, 8:31 PM   #12
Shalmanese
Member
 
Shalmanese's Avatar
 
Join Date: Jun 2001
Location: Seattle
Posts: 1,162
Default

Quote:
Originally posted by ludeking
I cant get any of your code to compile and run however I thunk I can see what you are getting at.
Hmm... I have a perl compiler now so I'm playing around with the code now. I can't figure out how to make the <STDIN> implicit in the first example, all the ones I've done were using while(<>) which does make it implicit.

for example:

Code:
while(<>) { print;}
is the equivilant of cat (although
Code:
print while (<>);
is shorter )

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";
If you want the one line version, its this:

Code:
print "The number of words in that string is: ", scalar(split(/\w+/,<>)) - 1, "\n\n" if print"\nPlease enter a string of text: ";;
ps: was looking through man perl and found this:

BUGS
The -w switch is not mandatory.

ROTFL!
Shalmanese is offline   Reply With Quote
Old 17th March 2004, 8:34 PM   #13
Shalmanese
Member
 
Shalmanese's Avatar
 
Join Date: Jun 2001
Location: Seattle
Posts: 1,162
Default

Quote:
Originally posted by ludeking
Argh,
I tried incorporating this:
Code:
@words = split(/\w+/,$text);
and I am still getting more than one white space being a word, plus chars like ? being a word, plus if there is only one word then $#words = 0!!!

Oh well, I'll move on and come back to it. Thanks for all your ideas anyway!!
this worked for me.

stuff like:

Code:
   sdfsdfsdf     ???!?!?!   sddsff   ^EOL
was reported as two words
Shalmanese is offline   Reply With Quote
Old 17th March 2004, 9:18 PM   #14
ludeking Thread Starter
Member
 
ludeking's Avatar
 
Join Date: Oct 2002
Posts: 1,561
Default

Quote:
Originally posted by Shalmanese
this worked for me.

stuff like:

Code:
   sdfsdfsdf     ???!?!?!   sddsff   ^EOL
was reported as two words
Yeah but in this assignment, anything that is not white space should count as a word.
I found the correct argument to split on.

Code:
split(/\s+/$variable)
will make my proggie do what I want, plus I removed all white space before splitting.
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");
and output
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.
ludeking is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 4:34 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!