Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Perl to count current value based on next value

  1. #1
    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. #2

    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. #3
    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. #4
    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. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    3,641
    Distro
    Ubuntu 10.04 Lucid Lynx

    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
    if you get a satisfactory answer to your problem, mark the thread as [SOLVED]. Thanks (How-to)

  6. #6
    Join Date
    Feb 2011
    Beans
    35

    Re: Perl to count current value based on next value

    Any idea to solve it?

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    3,641
    Distro
    Ubuntu 10.04 Lucid Lynx

    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.
    if you get a satisfactory answer to your problem, mark the thread as [SOLVED]. Thanks (How-to)

  8. #8
    Join Date
    Feb 2011
    Beans
    35

    Re: Perl to count current value based on next value

    Quote Originally Posted by Vaphell View Post
    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. #9
    Join Date
    May 2008
    Location
    UK
    Beans
    1,422
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Perl to count current value based on next value

    This is almost exactly the same problem as this thread :

    http://ubuntuforums.org/showthread.php?t=2103750

    and this one :

    http://ubuntuforums.org/showthread.php?t=2103383

    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.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  10. #10
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    3,641
    Distro
    Ubuntu 10.04 Lucid Lynx

    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)
    if you get a satisfactory answer to your problem, mark the thread as [SOLVED]. Thanks (How-to)

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •