in reply to Regex Substitution

Really, I think it would be a lot nicer/cleaner/simpler to do it in real code, but...the real question is, what do you do with zero? In the code below, it'll produce "Q" (just because), but it's easily modified to do anything else.
s/(-?\d+)/$1>0 ? "L" : ($1 == 0 ? "Q" : "S")/e;
Or fancier, but more cryptic
s/(-?\d+)/qw(Q L S)[$1 <=> 0]/e;

Replies are listed 'Best First'.
Re^2: Regex Substitution
by demerphq (Chancellor) on Dec 02, 2004 at 22:16 UTC
    s/(-)?(\d+)/ $1 ? "S" : $2 ? "L" : "Q" /e;

    Skips the numeric conversion part. :-)

    ---
    demerphq

Re^2: Regex Substitution
by TedYoung (Deacon) on Dec 02, 2004 at 20:56 UTC

    Hi,

    Just in case you need to support decimal numbers, here is an extended version of Eimi's answer:

    s/(-?\d+(\.\d+)?)/$1>0 ? "L" : ($1 == 0 ? "Q" : "S")/e; or s/(-?\d+(\.\d+)?)/qw(Q L S)[$1 <=> 0]/e;

    and add support of scientific notation:

    s/(-?\d+(\.\d+)?([Ee][+-]?\d+)?)/ ...

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re^2: Regex Substitution
by SamCG (Hermit) on Dec 02, 2004 at 21:14 UTC
    beautiful. . .

    I agree about the nicer/cleaner/simpler. . . I do feel that in addition to helping me with my immediate issue, however, my perl knowledge has been advanced -- thanks.

    And actually, what I do with zeros is set the result to null, which I can do before (or now, during) this operation.