LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-28-2003, 02:12 PM   #1
farhanali
LQ Newbie
 
Registered: Apr 2003
Location: Pakistan
Distribution: Ubuntu
Posts: 15

Rep: Reputation: 0
Post first perl program --need feedback


encouraged by the response to my previous post, "teaching myself perl -- need feedback " i am back with my code.

so this is a program that gives one word anagrams for any given word.
please give me feedback as to how it could be improved and comment about the coding style.
i am newbie at perl AND at programming so please forgive my ignorance.

PS: I have yet to learn references and OO perl!!
thanks in advance
Farhan Ali

example:
if the given word is "VEGETABLE"
then the anagrams are : BAT, TABLE, GAVE, LATE ... etc.

#-----------#
#anagrams.pl#
#-----------#
#!/usr/bin/perl


####the word for which we are searching anagrams####
print "What is the word? ";
my $input = lc <>;

###### if the user wants a particular alphabet######
##########to be present in all the words############
print "Is there an alphabet, that must be there? ";
my $compul = lc <>;
chomp $compul;

################minimum charecters reqd#############
print "Minimum number of alphabets in the answers? ";
my $number=<>;
chomp $number;


############the dictionary file to use##############
open FH, "/usr/share/dict/words" or die "Cant open word list!\n";

###########get the alphabets in an array############
my @inp_alphab = brkwrd($input);

#### we make a copy of the array bcz the will be####
######## destroyed when we check fo a match#########
my @inp_copy = @inp_alphab;


####################################################
####main loop! we check each word of dictionary#####
#### with the word. if the listword is a subset#####
####### of user given word we print the word #######
####################################################

while (<FH>)
{
my $dict = $_;
@inp_alphab = @inp_copy;
my @listword = brkwrd($dict);

for($i=0;$i le $#inp_alphab;++$i)
{
for ($j=0;$j le $#listword;++$j)
{
if ($inp_alphab[$i] eq $listword[$j])
{
#if the words match then mark them.
$inp_alphab[$i] =1;
$listword[$j] = 1;
}
}
}
# here we count the marked words. if equal to the length of
# the array and is greater than or eq to min chars and it includes
# the cumpulsory charector then print the word.

$count = total(@listword);
if (($count == ($#listword+1))
and ($count ge $number)
and ($dict =~/$compul/i))
{
print "$dict";
}
}
close FH;


############################################
############### ################
#### SUBROUTINES ####
############### ################
############################################


############################################
# A function that breaksup the string that #
# it recieves as an argument. #
############################################
sub brkwrd
{
my $intake = shift;
$intake =~ s/\s//g;
my @alphab_array = sort(split(//,$intake));
return @alphab_array;
}


############################################
# A function that returns the total of the #
# array. its is done to check for a match. #
############################################

sub total()
{
my @arr=@_;
my $asd =0;
for (my $loop=0 ; $loop le $#arr ; ++$loop)
{
$asd = $asd + $arr[$loop];
}
return $asd;
}
 
Old 06-28-2003, 02:24 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
ack... i meant put it in the *same* thread...
 
Old 08-03-2003, 11:10 AM   #3
kbray
LQ Newbie
 
Registered: Aug 2003
Posts: 2

Rep: Reputation: 0
farhanali,

I can help you out if youre looking for some perl help. Give me an email at kirk@ksu.edu if you want to, and Ill try to help you out.
 
Old 08-03-2003, 10:47 PM   #4
AquamaN
Member
 
Registered: Oct 2002
Location: Ohio, USA
Distribution: OS X 10.4.8, Ubuntu 6.10
Posts: 146

Rep: Reputation: 15
kbray,
ksu.edu, that stand for Kent State University in Ohio??

-AquamaN
 
Old 08-04-2003, 09:53 AM   #5
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
farhanali:

First, let me say that any time you post code on the site, please wrap it with [code] [/code] tags. This will preserve the indentation and make reading the code much easier for us!

Secondly, let me commend you on the use of variable declarations (using my var_name)! So many people don't do that, and IMHO, it's important, because it keeps the variable name usage in check, meaning it helps the coder and someone reading the code know when new variables are being added, and IMHO is just cleaner code.

Next, I would highly suggest that you always use the pragmas strict and warnings, as they will help you to write cleaner code, forcing certain standards (you'll understand that more as you code more, consider them as english teachers, but for perl). They are used as:
Code:
#!/usr/bin/perl
use strict;
use warnings;

... rest of code
As you have learned about the $_ perl variable, there is another one that is nice to have around. It is the $! error variable. Many functions put error messages into this variable when they fail. open is one of them. It's not necessary to have, but it could be more informative to the user of the script, if you included it in the open/die statement:

Code:
open FH, "/usr/share/dict/words" or die "Cant open word list: $!\n";
Not totally necessary, but nice to know why the open failed.

Your overall usage of perl is quite good, considering that this is your first real script.

Something to consider about this approach, though, is that it is a little bit slow. It's not bad, but loading the dictionary into memory and then duplicating it is generally not a good idea.... just because of the memory usage.

Here's another approach for you to consider with the script:

Open the dictionary and loop through it one line at a time. Make a copy of the dictionary item (single word). Then, for each letter in the input word, remove 1 of it from the dictionary word. If there aren't any letters left in the dictionary word, then you know it's an anagram.

There may be a better way, but I was simply looking for an option that would increase the speed of the program, as well as reduce memory usage.

If you'd like to see my version of the code, please ask.

TLD
 
Old 08-04-2003, 08:23 PM   #6
kbray
LQ Newbie
 
Registered: Aug 2003
Posts: 2

Rep: Reputation: 0
Quote:
Originally posted by AquamaN
kbray,
ksu.edu, that stand for Kent State University in Ohio??

-AquamaN
nah its Kansas State University.

I did live in columbus for a year and a half though... hehe
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Capturing program feedback... (not sure what to call it) CrazyPilot Programming 3 03-20-2005 11:10 AM
Perl program control MWFlint Programming 4 12-08-2004 12:59 PM
running a program from perl surban99 Linux - Software 1 06-22-2004 02:54 PM
teaching myself perl -- need feedback farhanali Programming 2 06-28-2003 11:36 AM
Please check this program(perl) for me! Rex_chaos Programming 2 04-25-2002 06:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration