# Thread: Perl to count current value based on next value

1. 5 Cups of Ubuntu
Join Date
Feb 2011
Beans
35

## Perl to count current value based on next value

Currently I'm learning Perl and gnuplot. I would like to know how to count certain value based on the next value. For example:
Code:
```#ID(X) Y
1 1
3 9
5 11```
The output should show the value of the unknown ID as well. So, the output should show:
Code:
```#ID(X) Y
1 1
(2) (5)
3 9
(4) (10)
5 11```
The Y of ID#2 is based on the following:
((2-3)/(1-3))*1 + ((2-1)/(3-1))*9 which is
((X2-X3)/(X1-X3))*Y1 + ((X2-X1)/(X3-X1)) * Y3
Same goes to ID#5.

2. Fresh Brewed Ubuntu
Join Date
Feb 2009
Beans
1,212

## Re: Perl to count current value based on next value

I suggest you write some Perl code to do it for you.

You have a formula, you know how to apply it to an input to get the desired output -- write an algorithm! It's not that hard, really. If you get stuck, post what you have.

3. 5 Cups of Ubuntu
Join Date
Feb 2011
Beans
35

## Re: Perl to count current value based on next value

I'm wondering how to get the next ID

4. 5 Cups of Ubuntu
Join Date
Feb 2011
Beans
35

## Re: Perl to count current value based on next value

This is what I have for current
#! /usr/bin/perl -w
use strict;

my \$prev_id = 0;
my \$prev_val = 0;
my \$next_id;
my \$next_val;

while (<>)
{
my (\$id, \$val) = split;
for (my \$i = \$prev_id + 1; \$i < \$id; \$i++)
{
\$val = ((\$id - \$next_id) / (\$prev_id - \$next_id)) * \$prev_val + ((\$id - \$prev_id) / (\$next_id - \$prev_id)) * \$next_val;
printf ("%d %s\n", \$i, \$val);
}
printf ("%d %s\n", \$id, \$val);
(\$prev_val, \$prev_id) = (\$val, \$id);
}

But how to declare the next value???

5. ## Re: Perl to count current value based on next value

aren't you overwriting \$val there inside the loop, just after reading from file? sloppiness doesn't have place in programming

6. 5 Cups of Ubuntu
Join Date
Feb 2011
Beans
35

## Re: Perl to count current value based on next value

Any idea to solve it?

7. ## Re: Perl to count current value based on next value

don't overwrite \$val and use separate variable to calculate in-between values?
you seem to not understand the purpose of your own variables.

have you tried:
- using seperate var for this line: \$val = ((\$id - \$next_id) / (\$prev_id - \$next_id)) * \$prev_val + ((\$id - \$prev_id) / (\$next_id - \$prev_id)) * \$next_val; or even plugging the expression directly into printf() so you don't overwrite actual \$val needed later?
- cleaning up the mess with variables that are used but never get any value, like \$prev_id, \$next_val, \$next_id, and vice versa - \$i not used anywhere? Your code doesn't use \$i at all and it's the only thing that changes in the for-loop scope, you have to use it.
Decide if you want to use \$i or \$temporary_intermediary_id as the loop controller or whatever and stick to it.

Code:
```#! /usr/bin/perl -w
use strict;

my (\$prev_id, \$prev_val) = (0,0);

while (<>)
{
my (\$id, \$val) = split;
for( my \$temp_id=\$prev_id+1; \$prev_id>0 && \$temp_id<\$id; \$temp_id++ )
{
printf( "*%s is between %s(%s) and %s(%s) <- all numbers required to generate value, use them!\n", \$temp_id, \$prev_id, \$prev_val, \$id, \$val );
}
printf( "%d %s\n", \$id, \$val );
(\$prev_id, \$prev_val) = (\$id, \$val);
}```
Code:
```\$ ./num2.pl <<< \$'1 1\n5 17\n9 25'
1 1
*2 is between 1(1) and 5(17) <- all numbers required to generate value, use them!
*3 is between 1(1) and 5(17) <- all numbers required to generate value, use them!
*4 is between 1(1) and 5(17) <- all numbers required to generate value, use them!
5 17
*6 is between 5(17) and 9(25) <- all numbers required to generate value, use them!
*7 is between 5(17) and 9(25) <- all numbers required to generate value, use them!
*8 is between 5(17) and 9(25) <- all numbers required to generate value, use them!
9 25```
Last edited by Vaphell; January 16th, 2013 at 02:17 PM.

8. 5 Cups of Ubuntu
Join Date
Feb 2011
Beans
35

## Re: Perl to count current value based on next value

Originally Posted by Vaphell
don't overwrite \$val and use separate variable to calculate in-between values?
you seem to not understand the purpose of your own variables.

have you tried:
- using seperate var for this line: \$val = ((\$id - \$next_id) / (\$prev_id - \$next_id)) * \$prev_val + ((\$id - \$prev_id) / (\$next_id - \$prev_id)) * \$next_val; or even plugging the expression directly into printf() so you don't overwrite actual \$val needed later?
- cleaning up the mess with variables that are used but never get any value, like \$prev_id, \$next_val, \$next_id, and vice versa - \$i not used anywhere? Your code doesn't use \$i at all and it's the only thing that changes in the for-loop scope, you have to use it.
Decide if you want to use \$i or \$temporary_intermediary_id as the loop controller or whatever and stick to it.

Code:
```#! /usr/bin/perl -w
use strict;

my (\$prev_id, \$prev_val) = (0,0);

while (<>)
{
my (\$id, \$val) = split;
for( my \$temp_id=\$prev_id+1; \$prev_id>0 && \$temp_id<\$id; \$temp_id++ )
{
printf( "*%s is between %s(%s) and %s(%s) <- all numbers required to generate value, use them!\n", \$temp_id, \$prev_id, \$prev_val, \$id, \$val );
}
printf( "%d %s\n", \$id, \$val );
(\$prev_id, \$prev_val) = (\$id, \$val);
}```
Code:
```\$ ./num2.pl <<< \$'1 1\n5 17\n9 25'
1 1
*2 is between 1(1) and 5(17) <- all numbers required to generate value, use them!
*3 is between 1(1) and 5(17) <- all numbers required to generate value, use them!
*4 is between 1(1) and 5(17) <- all numbers required to generate value, use them!
5 17
*6 is between 5(17) and 9(25) <- all numbers required to generate value, use them!
*7 is between 5(17) and 9(25) <- all numbers required to generate value, use them!
*8 is between 5(17) and 9(25) <- all numbers required to generate value, use them!
9 25```
How about the interpolation part? Any idea?

9. ## Re: Perl to count current value based on next value

This is almost exactly the same problem as this thread :

and this one :

The first one is marked as solved.

One of the arts of programming is re-use - so adapt the solution previously provided.
Last edited by Tony Flury; January 17th, 2013 at 03:03 PM.

10. ## Re: Perl to count current value based on next value

cheesus, do you really need the expression spelled out?
Code:
`*6 is between 5(17) and 9(25)`
you have 5(17) < 6(x) < 9(25), assuming linear interpolation find x.
hint: prev_id(prev_val) < temp_id(???) < id(val)