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

I am working on something related to texture analysis, the input is basically a string of digits. One of the things that such a analyzer should be able to do, is to recognize certain patterns, and process it.

A simplified example could be, if you see a pattern that could be split into two numbers, between which the second one doubles the first one, then their sum should replace the original pattern.

I used e1 and e2 to represent the two functions, which is, in my real work, much more complex. I made a general regexp as bellow. I want to know whether there is a better way, and whether anyone see any potential performance improvement:
sub e1 { shift() *2; } sub e2 { shift() + shift(); } $a = "12.2424.4812367"; $a =~ s/((?:\d+(?:\.\d+)?))((??{e1($+)}))/e2($1, $2)/ge; print $a;
In this case, the output should be: 36.72397. (36.72 is the sum of 12.24 and 24.48, 3 is the sum of 1 and 2, 9 is the sum of 3 and 6, 7 is a non-match)

Replies are listed 'Best First'.
Re: Is this enough good, or can be improved?
by Zaxo (Archbishop) on Dec 04, 2002 at 03:38 UTC

    How do you know whether your $a is joined from (12.24, 24.4812367) or (12.242, 4.4812367)?

    I think you don't. Keep numeric data in an array and don't do arithmetic on strings. Your e1() and e2() subs are fine as arithmetic, so look at map or a loop construct.

    Update: Take a look at PDL for efficient numeric operations on arrays of data.

    After Compline,
    Zaxo

      Okay, as we chatted, the data is actually from satellite pictures, so it comes like this, a digit string. It is the analyzer's responsibility to mining and dig out certain textures/patterns and process/use the data, one of its application is to find and locate natural resources.

      It is impossible for the regexp to mess up 12.24 with 24.4812367, or mess 12.242 with 4.4812367. If you test the regexp, you will see it does well, and matches 12.24 with 24.48 as specified by function e1.

      Even better, if you look at the first two digits, although the second digit doubles the first digit, it will not mess up 1 with 2, as I am using greedy match, it will try find the longest possible substring that satisfies e1, so it will dig until find the pair of 12.24 and 24.48. This serves the purpose to reduce background noises.

      I changed e2 a bit, so it demos the purpose more clearly:
      sub e1 { shift() * 2; } sub e2 { "(".shift().",".shift().")"; } $a = "12.2424.4812367"; $a =~ s/((?:\d+(?:\.\d+)?))((??{e1($+)}))/e2($1, $2)/ge; print $a;
      The output would be (12.24,24.48)(1,2)(3,6)7

        perhaps this is better.

        • the subs are anonymous and scoped within the pattern
        • $1 is used instead of $+. why are you using $+ anyway?
        • the pattern is commented, increasing clarity
        #!/usr/bin/perl use strict; use warnings; $a = "12.2424.4812367"; $a =~ s/(?x) ( (?# match integer and floating point numbers ) (?: \d+ (?: \. \d+ )? ) ) ( (?# execute code, match result ) (??{ ## return twice the first argument sub{ shift() * 2 }->( $1 ) }) ) / ## print the arguments as a couple sub{ join( '', '(', shift(), ',', shift(), ')' ) }->( $1, $2 ) /ge; print $a;

        ~Particle *accelerates*