joshywashy has asked for the wisdom of the Perl Monks concerning the following question:

Hi So i have a text file stored in $process I am trying to match using /in mmgsdi_evt_cb/ The file contains the following strings 0x0 in mmgsdi_evt_cb mmgsdi X mmgsdi Y 0xd in mmgsdi_evt_cb 0x1a in mmgsdi_evt_cb 0xe in mmgsdi_evt_cb 0xf in mmgsdi_evt_cb mmgsdi Z 0x13 in mmgsdi_evt_cb mmgsdi Z mmgsdi Z

the basic format of my loop is as follows
while<$process> { if (/in mmgsdi_evt_cb/) { print "match found"; } elsif(/something else/) { print "found something else"; } } print "finish";

The problem is if i search for just /0xf in mmgsdi_evt_cb/, my program terminates out of the while loop. if i search for just mmgsdi, the program finds all the /mmgsdi/ message EXCEPT the 0x13 string if i search for just /0x13/ it finds all the other 0x13 message EXCEPT the 0x13 in mmgsdi_evt_cb I want. Is there some limitation I am not aware of?

Replies are listed 'Best First'.
Re: Perl not recognizing a simple string
by Athanasius (Archbishop) on Feb 22, 2014 at 05:55 UTC

    Hello joshywashy,

    Without <code>...</code> tags around your data, we can only guess at how it’s formatted. If each each record occupies a separate line, like this:

    0x0 in mmgsdi_evt_cb mmgsdi X mmgsdi Y 0xd in mmgsdi_evt_cb 0x1a in mmgsdi_evt_cb ...

    then your approach should work fine (once the syntax error is fixed, of course). But if, as I suspect, the data is all on one line, then you need a more sophisticated strategy. Here is one approach:

    #! perl use strict; use warnings; my ($process) = <DATA>; # slurp in all the data at once # Use a look-ahead assertion while ($process =~ /(0x[0-9a-f]{1,2})(.*?)(?=0x[0-9a-f]{1,2}|\Z)/gi) { my ($hex, $text) = ($1, $2); print "Match found: $hex$text\n" if $text =~ /in mmgsdi_evt_cb/; } print "Finished\n"; __DATA__ 0x0 in mmgsdi_evt_cb mmgsdi X mmgsdi Y 0xd in mmgsdi_evt_cb 0x1a in mm +gsdi_evt_cb 0xe in mmgsdi_evt_cb 0xf in mmgsdi_evt_cb mmgsdi Z 0x13 i +n mmgsdi_evt_cb mmgsdi Z mmgsdi Z

    Output:

    15:31 >perl 882_SoPW.pl Match found: 0x0 in mmgsdi_evt_cb mmgsdi X mmgsdi Y Match found: 0xd in mmgsdi_evt_cb Match found: 0x1a in mmgsdi_evt_cb Match found: 0xe in mmgsdi_evt_cb Match found: 0xf in mmgsdi_evt_cb mmgsdi Z Match found: 0x13 in mmgsdi_evt_cb mmgsdi Z mmgsdi Z Finished 15:51 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Perl not recognizing a simple string
by kcott (Archbishop) on Feb 23, 2014 at 04:00 UTC

    G'day joshywashy,

    As already pointed out (either directly or via link), in order for us to help you, you need to provide:

    • The input data in a form that shows us exactly what it looks like.
    • The actual code you're running so we can also run it.
    • The actual output your code is producing (including any error or warning messages).
    • The expected output, i.e. what you really wanted.

    In the code you posted, you have a definite problem with "while<$process>". I suspect you also have a problem with "elsif(/something else/)".

    Is this closer to what you were trying to achieve:

    #!/usr/bin/env perl use strict; use warnings; while (<DATA>) { if (/in mmgsdi_evt_cb/) { print 'MATCH: ', $_; } else { print 'OTHER: ', $_; } } __DATA__ 0x0 in mmgsdi_evt_cb mmgsdi X mmgsdi Y 0xd in mmgsdi_evt_cb 0x1a in mmgsdi_evt_cb 0xe in mmgsdi_evt_cb 0xf in mmgsdi_evt_cb mmgsdi Z 0x13 in mmgsdi_evt_cb mmgsdi Z mmgsdi Z

    Output:

    MATCH: 0x0 in mmgsdi_evt_cb OTHER: mmgsdi X OTHER: mmgsdi Y MATCH: 0xd in mmgsdi_evt_cb MATCH: 0x1a in mmgsdi_evt_cb MATCH: 0xe in mmgsdi_evt_cb MATCH: 0xf in mmgsdi_evt_cb OTHER: mmgsdi Z MATCH: 0x13 in mmgsdi_evt_cb OTHER: mmgsdi Z OTHER: mmgsdi Z

    -- Ken

Re: Perl not recognizing a simple string (bullshit
by Anonymous Monk on Feb 22, 2014 at 01:26 UTC
      *gah* a warning about the title would have been nice, did not mean to post that *embarrassed*
        Bullshit is embarrassing?

        (fascinating ... my Swype dictionary is too polite to know "shit" or "bullshit")

        Cheers Rolf

        ( addicted to the Perl Programming Language)