ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You have a huge mess in your variables and the logic of your code. You use the values of the variables before you initialize them. You use the wrong variables to hold the wrong values.
What is $val and $id?
What is $prev_val and $prev_id?
What is $next_val and $next_id?
also, you should put a line there to handle the first line (comment)
btw, a very creative way of computing linear interpolation.
You have a huge mess in your variables and the logic of your code. You use the values of the variables before you initialize them. You use the wrong variables to hold the wrong values.
What is $val and $id?
What is $prev_val and $prev_id?
What is $next_val and $next_id?
also, you should put a line there to handle the first line (comment)
btw, a very creative way of computing linear interpolation.
$ID is the current ID that need to fill in, it probably exists or does not exist.
$val is the current value that need to fill in, it probably exists or does not exist.
$prev_val is the previous existing value.
$prev_ID is the previous existing ID.
$next_val is the next existing value.
$next_ID is the next existing ID.
The basic idea behind this is OK, but how comes nothing here depends on the variable $i you iterate over? You just keep computing this with the same set of values each time.
Also, $next_id and $next_val are not defined yet (at least in the first iteration).
Code:
($prev_val, $prev_id) = ($val, $id);
What are the values of $val and $id at this point?
The basic idea behind this is OK, but how comes nothing here depends on the variable $i you iterate over? You just keep computing this with the same set of values each time.
Also, $next_id and $next_val are not defined yet (at least in the first iteration).
Code:
($prev_val, $prev_id) = ($val, $id);
What are the values of $val and $id at this point?
Code:
($next_val, $next_id) = ($prev_val, $prev_id);
What is your reasoning behind this line?
$i basically used for the reformed ID.
the main problem is $next_id and $next_val because it depends on the next existing value.
the $val and $id is the existing value and existing ID.
Think about your algorithm a bit.
What you need for each interval, is id and value for both
1) the begining of the interval (you need to remember that from the previous iteration) and
2) the end of the interval (which is what you have just read from the input file).
In the for loop, you interpolate values for all ids between the begining and end of the interval.
In the end you just need to remember the last id and value processed, which happens to be the end of the interval. These will become the begining of the interval for the next iteration.
That's all there is to it. Don't make it more complicated than it needs to be.
Think about your algorithm a bit.
What you need for each interval, is id and value for both
1) the begining of the interval (you need to remember that from the previous iteration) and
2) the end of the interval (which is what you have just read from the input file).
In the for loop, you interpolate values for all ids between the begining and end of the interval.
In the end you just need to remember the last id and value processed, which happens to be the end of the interval. These will become the begining of the interval for the next iteration.
That's all there is to it. Don't make it more complicated than it needs to be.
Here is the updated one... Any Idea?
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);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.