in reply to Re: Is this enough good, or can be improved?
in thread Is this enough good, or can be improved?

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

Replies are listed 'Best First'.
Re^3: Is this enough good, or can be improved?
by particle (Vicar) on Dec 04, 2002 at 15:14 UTC

    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*