regexp question

S

Sam

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
 
T

Tore Aursand

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. :)
 
P

Patty Reynolds

"Tore Aursand" wrote
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
 
T

Tore Aursand

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).
 
S

Sam

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
 
G

gnari

Sam said:
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
 
J

jonathan adams

Sam said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

small regexp help 1
Data saving in condition of changing reality 0
Loop over regexp groups 8
Regexp 12
a simple RegExp question 5
regexp with accent insensitive ?? 3
The Towers of Hanoi 3
Regexp golf question 4

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top