Home: Perl Programming Help: Beginner:
Post deleted by stukov114



stukov114
Novice

Apr 16, 2009, 2:03 PM


Views: 1796
Post deleted by stukov114

 


KevinR
Veteran


Apr 16, 2009, 2:43 PM


Views: 1793
Re: [stukov114] Splitting up a string and comparing

If I were you I might look into index(). You will split the word into an array and loop through each letter in the word and find its position in the message. As long as they are all found and the position of each letter is higher than the last I guess you can assume the secret word is in the message. Note I have tried to write any code to test my suggestion.
-------------------------------------------------


stukov114
Novice

Apr 16, 2009, 6:28 PM


Views: 1788
Post deleted by stukov114

 


KevinR
Veteran


Apr 16, 2009, 6:44 PM


Views: 1785
Re: [stukov114] Splitting up a string and comparing

Sorry, I am not following your description. Maybe some samples will help understand what you are asking.
-------------------------------------------------


stukov114
Novice

Apr 17, 2009, 2:53 AM


Views: 1775
Post deleted by stukov114

 


KevinR
Veteran


Apr 17, 2009, 10:23 AM


Views: 1753
Re: [stukov114] Splitting up a string and comparing

See how this works:


Code
my $word = 'the'; 
my $text = 'tiger horse evict';
if (find_secret($word,$text)) {
print "Found the secret message!\n";
}
else {
print "Did not find the secret message!\n";
}

sub find_secret {
my $word = $_[0];
my $text = $_[1];
my @letters = split(//,$word);
my $pos = index($text,$letters[0],0);#find position of first letter
return(0) if $pos < 0;
foreach my $i (1..$#letters) {#find the next of the letter in relationship to the previous letter
my $p = index($text,$letters[$i],($pos+1));
if ($p < 1 || $p > 25) {
return(0);# out of range so stop looking and return failure
}
}
return(1);#success
}

-------------------------------------------------


KevinR
Veteran


Apr 17, 2009, 10:29 AM


Views: 1752
Re: [stukov114] Splitting up a string and comparing

Previously you said:


Quote
I think i could do this by placing the message in a string and iterate though the large text but there must
be some shorter way using regular expression.


Don't confuse "short" with "better". I think the code I posted is better than trying to use regexp, it is almost certainaly more efficient and readable, although bit shifting might be even faster, but I am not so good with the bit-shifting operators.
-------------------------------------------------


stukov114
Novice

Apr 17, 2009, 5:54 PM


Views: 1743
Post deleted by stukov114

 


KevinR
Veteran


Apr 17, 2009, 9:24 PM


Views: 1729
Re: [stukov114] Splitting up a string and comparing

Without knowing what the spacing should be beforehand that could be ridiculously hard.
-------------------------------------------------


stukov114
Novice

Apr 18, 2009, 5:07 AM


Views: 1722
Re: [KevinR] Splitting up a string and comparing

ok, thanks for the help!