Pattern detection and until loop in perl

Discussion in 'Software' started by vrushali, Dec 10, 2010.

  1. vrushali

    vrushali

    Joined:
    Dec 9, 2010
    Messages:
    2
    Hi,
    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..


    #!/usr/bin/perl
    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: Dec 10, 2010
    vrushali, Dec 10, 2010
    #1
    1. Advertising

  2. vrushali

    sbglobal2010

    Joined:
    Dec 15, 2010
    Messages:
    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;}
    [download]

    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
    [download]

    unless and until can sometimes confuse folks, so I tend to prefer using "while" for the vast majority of my loops like this.
     
    sbglobal2010, Dec 15, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan Eales

    A pattern emerging for wireless and SP2

    Jonathan Eales, Sep 22, 2004, in forum: Wireless Networking
    Replies:
    7
    Views:
    1,618
    DrBol
    Nov 2, 2004
  2. Dustin
    Replies:
    0
    Views:
    428
    Dustin
    Jul 27, 2006
  3. aung.naingoo@gmail.com
    Replies:
    1
    Views:
    11,607
    Phillip Remaker
    May 9, 2007
  4. business one way

    Is it a Perl program or a Perl script?

    business one way, Jan 5, 2008, in forum: Digital Photography
    Replies:
    0
    Views:
    763
    business one way
    Jan 5, 2008
  5. C#
    Replies:
    3
    Views:
    406
    Peter Morris
    Aug 1, 2008
Loading...

Share This Page