Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

My first Perl!

Sign in to follow this  


I decided today to work on one of our first homework problems in Programming Languages. The idea isn't bad: given user input find out if the word is an isogram (which from my understanding means that each letter in the word is repeated the same number of times). You just display a message saying what "order" it is (probably the wrong wording, but I can't think of anything better). If it has non word characters we can ignore it.

This is the first Perl program i've written with any substance. It's not too bad, but it's longer than I would've hoped it would be ~45 lines. Seems long, but it does the job.

while($input ne "end")
print "Enter a word to find the isogram (end to exit): ";
$input = ;
$input = "\L$input";

if($input =~ /\W/)
print $input . " has an illegal character.\n";
else {
@allchars = split//,$input;
@allchars = sort(@allchars);

my %hash = ();

for($i=0; $i<=$#allchars; $i++)
$hash{ @allchars[$i] }++;

$zVal = $hash{@allchars[0]};

$count = 0;
while( ($key, $value) = each(%hash))
if($value == $zVal){

if($count == (keys %hash))
print "$input is a $hash{@allchars[0]} letter isogram.\n";
print "$input is not an isogram.\n";

So, for example, if you enter TartAr (an example we were provided) the program will output that it is a 2 letter isogram since t, a, and r all occur twice. It's not bad, but the syntax after coming from C++ can be a bit weird. I'm sure there's a more efficient way to do it, but I don't know it [smile].
Sign in to follow this  


Recommended Comments

Hmm, yeah, according to that definition it might be wrong, but based on the example we were given in class (and the explanation she gave) I think it does what it's supposed to do. I'll ask though. Thanks.

Edit: Just looked at the paper and it seems to contradict the Wikipedia article. As a test case the string Aqwsssaqwaqwswqa works out to be a 4 letter isogram. Our professor didn't write the problem description, she got it from somewhere else, so maybe the original author misused the word.

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!