qball has asked for the wisdom of the Perl Monks concerning the following question:

My goal is to retrieve the last two rows of a csv file and do a calculation on the unix timestamp..the idea is to get a % OnTime and print the percentage of that time. The csv example rows are as follows:
sequence,timestamp,received,sent,status 1,997891093,,Sent^M 2,997891093,,Sent^M 1,997891394,,Sent^M 3,997891394,,Sent^M 1,997891694,,Sent^M 4,997891694,,Sent^M 1,997897224,,Sent^M 5,997897224,,Sent^M 1,997897546,,Sent^M 6,997897546,,Sent^M 1,997897847,,Sent^M 7,997897847,,Sent^M 1,997898147,,Sent^M 8,997898147,,Sent^M 1,997898448,,Sent^M 9,997898448,,Sent^M 1,997898749,,Sent^M 10,997898749,,Sent^M 11,997899354,,Sent^M 12,997899654,,Sent^M 13,997899955,,Sent^M 14,997900256,,Sent^M 15,997900556,,Sent^M 16,997900860,,Sent^M 17,997901160,,Sent^M 18,997901462,,Sent^M 19,997901763,,Sent^M 20,997902064,,Sent^M 21,997902366,,Sent^M 22,997902667,,Sent^M 23,997902968,,Sent^M

I want to get the percentage of time taken between the last two unix timestamps in the last two rows and print that percentage as follows:

If anything is unclear, please let me know as I am happy to answer any questions.


qball~"I have node idea?!"

Replies are listed 'Best First'.
(elbie): Last two rows or a csv file.
by elbie (Curate) on Aug 18, 2001 at 00:18 UTC
    Getting the last couple of rows is easy enough. Here's an example fragment that just reads the file in from STDIN:

    $my( $curr_time, $prev_time ); while( <> ) { $prev_time = $curr_time; $curr_time = ( split /,/ )[1]; }

    You can modify that to be a bit more robust or to open a file properly or what have you.

    I'm not sure how you're calculating the percentage. Are you just dividing one number by the other?

    You can print the percentage in whatever format you need using sprintf, for example:

    $percentage = "%" . sprintf( "%.3f", $prev_time / $curr_time * 100 );

    Or whatever your calculation is.

    I'm sorry I can't be of much more help than this. If your code isn't working properly, post the fragment where the problem lies (or a test program that duplicates the problem) here and we'll try and sort through it.

    If you don't have any code yet, then try your hand at the problem yourself. Worst case scenario is you're no worse off than before. :)

    Good luck!


      This is what I've got:
      #!/usr/bin/perl open (FILE, "edidocs.csv") or die "Can't open file $!\n"; @lines = <FILE>; while ($line = pop @lines) { print $line "\n"; }
      It prints the rows from last to first. Now I need to get the first two rows timestamp and do the calulations. Then we'll be good to go!

      qball~"I have node idea?!"
        If you don't need to store the contents of the file, try this:

        open (FILE, "edidocs.csv") or die "Can't open file $!\n"; my( $curr_time, $prev_time ); # Sorry about the $ in the original while( <FILE> ) { $prev_time = $curr_time; $curr_time = ( split /,/ )[1]; }

        If you do need to store the whole file, then the last two lines are stored in $lines( $#lines - 1 ) and $lines( $#lines ) of your example before you popped them off the stack.