Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Time Intervals

by Anonymous Monk
on Jun 27, 2012 at 20:23 UTC ( [id://978759]=perlquestion: print w/replies, xml ) Need Help??

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

Monks, I have an array in csv format. I need to select the row with the highest value within each 5 minute interval. Intervals would be every 5 minutes, 10:00, 10:05, 10:10, etc. Below is the code I am working with.

10:39:34 759 10:43:34 735

What is the best way of doing this? Thank you.

Replies are listed 'Best First'.
Re: Time Intervals
by BrowserUk (Patriarch) on Jun 27, 2012 at 20:40 UTC

    Untested:

    perl -F\t -anle"($t)=$F[0]=~/^(.{5})/;$t=~s/(.)$/$1<5?0:5/e; $t{$t}=$F +[1] if $F[1]>$t{$t} }{ print qq[$_\t$t{$_}] for sort keys %t" file.cs +v

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: Time Intervals
by johngg (Canon) on Jun 27, 2012 at 22:13 UTC

    Construct a hash of arrays keyed by value in the file, containing arrays of lines for each value in case there is more than one time period with the same value. Use List::Util->max to find the largest key in the hash and print the line(s) associated with it.

    knoppix@Microknoppix:~$ perl -Mstrict -Mwarnings -MList::Util=max -E ' > open my $fh, q{<}, \ <<EOD or die $!; > 10:38:34 759 > 10:43:34 735 > 10:48:34 817 > 10:53:34 648 > 10:58:34 817 > 11:03.34 774 > EOD > > my %byValue; > while ( <$fh> ) > { > my $value = ( split )[ 1 ]; > push @{ $byValue{ $value } }, $_; > } > > print for @{ $byValue{ max keys %byValue } };' 10:48:34 817 10:58:34 817 knoppix@Microknoppix:~$

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Time Intervals
by jwkrahn (Abbot) on Jun 28, 2012 at 00:55 UTC
    #!/usr/bin/perl use warnings; use strict; open FIN, '<', 'something.csv' or die "Cannot open 'something.csv +' because: $!"; open FOUT, '>', 'something_new.csv' or die "Cannot open 'something_new +.csv' because: $!"; my %data; while ( <FIN> ) { next unless /^(\d+):(\d+):\d+\s+(\d+)$/; my $key = sprintf '%02d%02d', $1, int( $2 / 5 ) * 5; if ( exists $data{ $key } ) { $data{ $key } = [ $., $_, $3 ] if $data{ $key }[ 2 ] < $3; } else { $data{ $key } = [ $., $_, $3 ]; } } print FOUT map $_->[ 1 ], sort { $a->[ 0 ] <=> $b->[ 0 ] } values %dat +a;
Re: Time Intervals
by Anonymous Monk on Jun 27, 2012 at 20:26 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://978759]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-24 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found