regexp question

Discussion in 'Perl Misc' started by Sam, Nov 16, 2004.

  1. Sam

    Sam Guest

    Hello,

    I'm have the following problem: I have pair of numbers like 111.1234 and
    111.1235. But I want to display that than as 111.1234|5 (to save space on
    the screen).
    Other examples:

    111.98 and 114.0 -> 111.98|4.0
    1.2345 and 1.2945 -> 1.2345|945

    I have sort of a solution but it's quiet ugly, maybe someone has an idea for
    an elegant solution.

    Thanks,
    Sam
     
    Sam, Nov 16, 2004
    #1
    1. Advertising

  2. Sam

    Tore Aursand Guest

    On Tue, 16 Nov 2004 16:09:54 +0000, Sam wrote:
    > I'm have the following problem: I have pair of numbers like 111.1234 and
    > 111.1235. But I want to display that than as 111.1234|5 (to save space
    > on the screen).
    > Other examples:
    >
    > 111.98 and 114.0 -> 111.98|4.0
    > 1.2345 and 1.2945 -> 1.2345|945


    If you're only dealing with two numbers at a time, you should store the
    difference between the numbers instead; I refuse to believe that your
    screen is _that_ small. :)


    --
    Tore Aursand <>
    "Leadership is doing what is right when no one is watching." (George
    Van Valkenburg)
     
    Tore Aursand, Nov 16, 2004
    #2
    1. Advertising

  3. "Tore Aursand" wrote
    > On Tue, 16 Nov 2004 16:09:54 +0000, Sam wrote:
    > > I'm have the following problem: I have pair of numbers like 111.1234 and
    > > 111.1235. But I want to display that than as 111.1234|5 (to save space
    > > on the screen).
    > > Other examples:
    > >
    > > 111.98 and 114.0 -> 111.98|4.0
    > > 1.2345 and 1.2945 -> 1.2345|945

    >
    > If you're only dealing with two numbers at a time, you should store the
    > difference between the numbers instead


    but with the difference he has still to deal with other problems:
    e.g 111.98 and 114.0
    would be 111.98|2.02 and not 111.98|4.0

    But I agree that you can not easily solve that with a regular expression.

    Patty
     
    Patty Reynolds, Nov 16, 2004
    #3
  4. Sam

    Tore Aursand Guest

    On Tue, 16 Nov 2004 18:56:05 +0000, Patty Reynolds wrote:
    >>> I'm have the following problem: I have pair of numbers like 111.1234
    >>> and 111.1235. But I want to display that than as 111.1234|5 (to save
    >>> space on the screen).
    >>> Other examples:
    >>>
    >>> 111.98 and 114.0 -> 111.98|4.0
    >>> 1.2345 and 1.2945 -> 1.2345|945


    >> If you're only dealing with two numbers at a time, you should store the
    >> difference between the numbers instead


    > but with the difference he has still to deal with other problems: e.g
    > 111.98 and 114.0
    > would be 111.98|2.02 and not 111.98|4.0


    I know that - I just wanted to give him another solution to his "space
    problem".

    As long as one knows the difference between two numbers, and knowing one
    of the numbers, you can always calculate the other number. The same can
    be done with more than two numbers, ie.:

    111.98|2.02|-3.2 ... etc ...

    First, calculate the sum of the two first numbers, then proceed one to the
    next "sequence" and so on. No need for a regular expression, and it is
    easier to maintain (especially if you some day _need_ to handle more than
    two numbers).


    --
    Tore Aursand <>
    "It's not so much what you have to learn if you accept weird theories,
    it's what you have to unlearn." (Isaac Asimov)
     
    Tore Aursand, Nov 16, 2004
    #4
  5. Sam

    Sam Guest

    > On Tue, 16 Nov 2004 18:56:05 +0000, Patty Reynolds wrote:
    > >>> I'm have the following problem: I have pair of numbers like 111.1234
    > >>> and 111.1235. But I want to display that than as 111.1234|5 (to save
    > >>> space on the screen).
    > >>> Other examples:
    > >>>
    > >>> 111.98 and 114.0 -> 111.98|4.0
    > >>> 1.2345 and 1.2945 -> 1.2345|945

    >
    > >> If you're only dealing with two numbers at a time, you should store the
    > >> difference between the numbers instead

    >
    > > but with the difference he has still to deal with other problems: e.g
    > > 111.98 and 114.0
    > > would be 111.98|2.02 and not 111.98|4.0

    >
    > I know that - I just wanted to give him another solution to his "space
    > problem".
    >
    > As long as one knows the difference between two numbers, and knowing one
    > of the numbers, you can always calculate the other number. The same can
    > be done with more than two numbers, ie.:
    >
    > 111.98|2.02|-3.2 ... etc ...
    >
    > First, calculate the sum of the two first numbers, then proceed one to the
    > next "sequence" and so on. No need for a regular expression, and it is
    > easier to maintain (especially if you some day _need_ to handle more than
    > two numbers).


    sorry for not being clear enough - I want to save these values in a column
    (therefor the space limitations) and I would like to have it a format like
    xx.xxx|yy so that you can see the values and not only the difference
     
    Sam, Nov 16, 2004
    #5
  6. Sam

    gnari Guest

    "Sam" <> wrote in message
    news:mDpmd.61$...
    > Hello,
    >
    > I'm have the following problem: I have pair of numbers like 111.1234 and
    > 111.1235. But I want to display that than as 111.1234|5 (to save space on
    > the screen).
    > Other examples:
    >
    > 111.98 and 114.0 -> 111.98|4.0
    > 1.2345 and 1.2945 -> 1.2345|945


    how can this work ?
    given 1.2345|945 , how do you differentiate between the pairs:
    1.2345 and 1.234945 -> 1.2345|945
    1.2345 and 1.23945 -> 1.2345|945
    1.2345 and 1.2945 -> 1.2345|945
    1.2345 and 1.945 -> 1.2345|945
    1.2345 and 1945 -> 1.2345|945
    1.2345 and 945 -> 1.2345|945
    ?

    gnari
     
    gnari, Nov 16, 2004
    #6
  7. "Sam" <> wrote in message news:<mDpmd.61$>...
    > Hello,
    >
    > I'm have the following problem: I have pair of numbers like 111.1234 and
    > 111.1235. But I want to display that than as 111.1234|5 (to save space on
    > the screen).
    > Other examples:
    >
    > 111.98 and 114.0 -> 111.98|4.0
    > 1.2345 and 1.2945 -> 1.2345|945
    >


    I came up with a solution that works for the examples you posted plus
    a few that I could think of.

    $second = join("\t", $first, $second);
    $second =~ s/^([\d.]+)[\d.]*?\t\1([\d.]*?)$/$2/;
    print "$first|$second\n";

    -jaa
     
    jonathan adams, Nov 17, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greg Hurrell
    Replies:
    4
    Views:
    167
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    510
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    381
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    196
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    192
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page