CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



stukov114
Novice

Apr 16, 2009, 2:03 PM

Post #1 of 10 (1600 views)
Post deleted by stukov114

 


KevinR
Veteran


Apr 16, 2009, 2:43 PM

Post #2 of 10 (1597 views)
Re: [stukov114] Splitting up a string and comparing [In reply to] Can't Post

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

Post #3 of 10 (1592 views)
Post deleted by stukov114 [In reply to]

 


KevinR
Veteran


Apr 16, 2009, 6:44 PM

Post #4 of 10 (1589 views)
Re: [stukov114] Splitting up a string and comparing [In reply to] Can't Post

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

Post #5 of 10 (1579 views)
Post deleted by stukov114 [In reply to]

 


KevinR
Veteran


Apr 17, 2009, 10:23 AM

Post #6 of 10 (1557 views)
Re: [stukov114] Splitting up a string and comparing [In reply to] Can't Post

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

Post #7 of 10 (1556 views)
Re: [stukov114] Splitting up a string and comparing [In reply to] Can't Post

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

Post #8 of 10 (1547 views)
Post deleted by stukov114 [In reply to]

 


KevinR
Veteran


Apr 17, 2009, 9:24 PM

Post #9 of 10 (1533 views)
Re: [stukov114] Splitting up a string and comparing [In reply to] Can't Post

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


stukov114
Novice

Apr 18, 2009, 5:07 AM

Post #10 of 10 (1526 views)
Re: [KevinR] Splitting up a string and comparing [In reply to] Can't Post

ok, thanks for the help!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives