Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Noob

Status
Not open for further replies.

blundellp

Technical User
Apr 6, 2005
25
GB
I have this perl program and i want to compare the letters of the alphabet against the letters i input. However when compare it says everyletter in the alphabet is a true match of every letter i input i.e. A = F . The line of code in question is

if (@letters[$letter] == @alphabet[$alpha])

My full code is posted below if the error lies somewere else, i know this is a proper childish question but i am new to perl and trying to understand. Thanks for any help given. Paul

#!/usr/bin/perl
#Create a Program that filters 500,000 words
#down to words only with certain letters in "A D E F G H K S U Z"
$var = <STDIN>; #16 at most
chomp($var);
@letters = split(/,/, $var);
@alphabet = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z); #26
$letter = 0;
$alpha = 0;
while ($alpha < 26)
{
$negletters = 0;
while ($negletters <16)
{
if (@letters[$letter] == @alphabet[$alpha])
{
@currentletter = splice(@alphabet,0,1);
#delete @alphabet[$alpha]; #delete the correspoding letter in alphabet
$negletters = 16;
}
else
{
$alpha ++;
$negletters ++;
}
}
$letter ++;
}
exit;
 
if you are comparing strings or alpha characters use 'eq' instead of '==' which is for comparing numbers.

string comparison operators:

eq
ne
lt
gt
le
ge

corresponding numeric comparison operators:

==
!=
<
>
<=
>=


- Kevin, perl coder unexceptional!
 
Couldn't you just run matches like this?:

m/[ADEFGHKSUZ]{1}/i
m/[ADEFGHKSUZ]{2}/i
m/[ADEFGHKSUZ]{3}/i
m/[ADEFGHKSUZ]{4}/i

etc? This would find words that only use those letters. You've got a good challenge there. I'm now trying to match words that necessarily use those letters but may or may not use other letters. Unfortunately it seems perl doesn't have any operator for this. It would have to be something like the [] thing, but with AND instead of OR and would have to look for them in any order of appearance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top