Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Keep It Simple, Stupid
 
PerlMonks  

read string (which is in binary) and make decision

by hey_frind (Initiate)
on Nov 17, 2012 at 08:26 UTC ( #1004288=perlquestion: print w/ replies, xml ) Need Help??
hey_frind has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a input file which is something like this

test 1: 000000 test 2: 111111 test 3: 000011111 test 4: 1111111000 test 5:10001001110000011 ..... so on

My requirement is i need to read each string (always in binary) and gives one of the third possible result. possible results are PASS, FAIL, CLEAN

Criteria is:

1) if the sting has all 0's or all 1's (like test 1, and test 2) I should output test 1, test 2 as PASS

2) if the string starts with 0 and continue to be 0's and then transitions to 1 and always stays 1 (like test 3), i would say result is CLEAN. and vice-versa (ie starts with 1 and transitions to 0 and stays 0 (like in test 4), I would result is CLEAN.

3) if the string is combinations of 0's and 1's (like test 5) i would sat result is FAIL.

How do I do this in perl? thanks, Natasha

Comment on read string (which is in binary) and make decision
Re: read string (which is in binary) and make decision
by CountZero (Chancellor) on Nov 17, 2012 at 08:39 UTC
    use Modern::Perl; while (<DATA>) { chomp; say "$_ : ", ( /^0+$/ or /^1+$/ ) ? 'PASS' : ( /^0+1+$/ or /^1+0+$/ ) ? 'CLEAN' : 'FAIL'; } __DATA__ 000000 111111 000011111 1111111000 10001001110000011
    Output:
    000000 : PASS 111111 : PASS 000011111 : CLEAN 1111111000 : CLEAN 10001001110000011 : FAIL

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
      Thank you very much Count Zero for prompt. Really appreciate the help. the code works great... I have used it in reg ex actually because my perl set up does not recongize Modern::Perl line. downloaded from CPAN , but then install was complaining on other modules missing (Build, Metadata....) . downloaded all of them, did install but still the install on Modern package complains. is there a single place where I can install the whole package? thanks Natasa
        What kind and version of Perl are you using on what OS?

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics

        It will be very helpful to your learning experience if you have access to a Perl installation that allows downloading and installation of CPAN modules. It will be worth some effort to achieve this.

        However, for the purposes of running CountZero's example code, the two most important modules ('pragmas' in Perlish) to use in place of Modern::Perl and at the beginning of all code are
            use warnings;
            use strict;
        (IMHO). If your Perl installation does not offer these fundamental modules, it should be considered seriously b0rken.

        In addition to using the above-mentioned modules (pragmata), you will also need to either replace the  say() function with some variation of the print built-in, e.g.
            print $string, 'string', ..., "\n";
        or else enable the feature via the feature pragma:
            use feature 'say';
        in Perl versions 5.10+.

Re: read string (which is in binary) and make decision
by rjt (Chaplain) on Nov 17, 2012 at 13:32 UTC

    I would favor something like this:

    while (<DATA>) { my ($test, $_) = /^(.+?):\s?([01]+)$/ or do { warn "Line $. is invalid: $_"; next }; print "$test $_: ", /^ (0+|1+) $/x ? 'PASS' : /^(1+0+|0+1+)$/x ? 'CLEAN' : 'FAIL', "\n"; } __DATA__ test 1: 000000 test 2: 111111 test 3: 000011111 test 4: 1111111000 test 5:10001001110000011 ..... so on

    This outputs:

    test 1 000000: PASS test 2 111111: PASS test 3 000011111: CLEAN test 4 1111111000: CLEAN test 5 10001001110000011: FAIL Line 6 is invalid: ..... so on
Re: read string (which is in binary) and make decision
by flexvault (Vicar) on Nov 17, 2012 at 16:26 UTC

    Welcome hey_frind,

      My requirement is i need to read each string (always in binary)

    Just a caution. The example strings represent binary information, but are actually ASCII characters for '0' and '1'. If the data your working with is in actual binary format, you may need to covert to ASCII characters to run the scripts. If that is the case then 'pack/unpack' will be your friend. Look at the 'b' and 'B' formats.

    Good Luck!

    "Well done is better than well said." - Benjamin Franklin

      Thank you every one with the wonderful feedback and helping me get started on perl'ing !

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1004288]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2014-04-17 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (439 votes), past polls