Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > General Computer Discussion > Software > Pattern detection and until loop in perl

Thread Tools

Pattern detection and until loop in perl

vrushali vrushali is offline
Junior Member
Join Date: Dec 2010
Posts: 2
I am accesing the serial port through perl script, I am trying to detect some pattern in the incoming serial port data (lets say i am trying to find "abcd" in incoming serial port data like "zcfgsdbabcdppuahjajg") and till the pattern is
detected i want to execute some command using until loop in perl and once the pattern is detected the perl script should come out of the loop and should execute the next command after the loop. But after detecting the pattern the perl script is executing the command inside the loop one more time. How can i stop the execution of the loop and come out of it after the pattern detection and send next command.

Here is the code that i am using..

use Win32::SerialPort;

$PortObj->are_match("abcd"); # Pattern to match
my $gotit = "";
until ("" ne $gotit) {
$gotit = $PortObj->lookfor(); # Look for the pattern "abcd" in the incoming data
if($gotit){last;} # to exit the loop after the pattern detection

print("Pattern is not detected\n"); # print the message till pattern is detected.
# send some command through serial port in loop till the pattern is detectd

# send next command after the pattern detection.

Last edited by vrushali; 12-10-2010 at 06:27 AM..
Reply With Quote
sbglobal2010 sbglobal2010 is offline
Junior Member
Join Date: Dec 2010
Posts: 6
Your code does appear to do what you are asking for so I find your question a bit confusing. However some simplifications are possible to make things more clear.

You have 2 ways to exit the loop:

- until ("" ne $gotit) - if($gotit){last;}

I would simplify to one condition. Note null string is "false". Also $gotit doesn't appear to be necessary because it appears that the lookfor() method is looking for what you told it to with the are_match() method.

$PortObj->are_match("abcd"); # Pattern to match send_prompt(); #I presume this happens? while (!$PortObj->lookfor() ) { print("Pattern is not detected\n"); send_prompt(); #re-prompt } # pattern is detected here ... we continue

unless and until can sometimes confuse folks, so I tend to prefer using "while" for the vast majority of my loops like this.
Reply With Quote

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Signal Transition detection - wait until... or if construct MJ VHDL 4 02-11-2008 01:56 PM
loop until keypress (Windows XP) placid Python 6 08-10-2006 11:06 PM
Loop until condition is true Remi Villatel Python 40 06-23-2005 07:21 PM
ANN: ruleCore 1.0 - Engine for event pattern detection Marco Python 0 02-19-2004 01:56 PM