Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: compare values while value on other column remains the same

by Anonymous Monk
on Sep 21, 2012 at 21:11 UTC ( #994992=note: print w/replies, xml ) Need Help??


in reply to compare values while value on other column remains the same

Something like this?
use strict; use warnings; use List::Util qw(sum); my %table; while (defined(my $line = <DATA>)) { my @values = split(/\s+/, $line); push @{$table{$values[3]}}, \@values; } foreach my $key (sort keys %table) { my $avg_diff = sum(map { ($_->[5] / 1000) - ($_->[4] / 1000) } @{$ +table{$key}}) / @{$table{$key}}; foreach my $array_ref (@{$table{$key}}) { if ($avg_diff >= 0.02) { print join("\t", @{$array_ref}), "\n"; } } } __DATA__ chr1 15865 15915 cg13869341 908 913 chr1 18827 18877 cg14008030 688 776 chr1 29407 29457 cg12045430 43 70 chr1 29407 29457 cg12045430 43 88 chr1 29407 29457 cg12045430 43 16 chr1 29425 29475 cg20826792 57 70 chr1 29425 29475 cg20826792 57 88 chr1 29425 29475 cg20826792 57 16 chr1 29435 29485 cg00381604 33 70 chr1 29435 29485 cg00381604 33 88 chr1 29435 29485 cg00381604 33 16 chr1 68849 68899 cg20253340 560 593 chr1 69591 69641 cg21870274 791 809 chr1 91550 91600 cg03130891 55 84

Replies are listed 'Best First'.
Re^2: compare values while value on other column remains the same
by linseyr (Acolyte) on Sep 26, 2012 at 20:23 UTC
    Hi, Thank you for your answer. I want to do something slightly different now, and Im trying to do it with the code you provided, but since Im all new with this, the hashes and maps are a bit difficult to me.. I want to do it at a different order, because I want to do something else with it. I already combined multiple data files into one which now looks like this:
    cg12045430 43 16 6 44 23 63 24 77 cg12045430 43 16 6 44 23 63 24 80 cg12045430 43 16 6 44 23 63 24 45 cg12045430 43 16 6 44 23 115 24 77 cg12045430 43 16 6 44 23 115 24 80 cg12045430 43 16 6 44 23 115 24 45 cg12045430 43 16 6 44 23 29 24 77 cg12045430 43 16 6 44 23 29 24 80 cg12045430 43 16 6 44 23 29 24 45
    So I have again the same id's on multiple lines and different values on columns 2-9. I want for one ID get the average of columns 2-9. So for that input file that would be:
    cg12045430 43 16 6 44 23 69 24 67
    And afterwards I want to calculate the difference between column 2,3 and 4,5 and 6,7 and 8,9 like before. I tried to do it using your code and for every key calculate the average of the map, but it didn't work. Could you give me some advise? Thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://994992]
help
Chatterbox?
[Discipulus]: good morning monks!
[marto]: Corion, I've been better, but things are improving in many cases :) Simply not enough time/rest :)
[Corion]: Hi Discipulus!
[marto]: hopefully you guys are also well :)
[Corion]: marto: Yeah - I'm looking for a week off in a week, even if it'll be a trip to Amsterdam ;)
[Discipulus]: oh thanks, i'm always at well as the ratchet or bucket
[Corion]: But I'm slowly starting to get into programming again - yesterday I translated my talk for YAPC::E (in Amsterdam) to English, and decided to put my Jekyll-clone to rest for the time being
[Corion]: Discipulus++ # being well is like being earnest
[marto]: hopefully next year I'll be in a position to go to Perly things in Europe :)
[Corion]: Maybe I'll use the Real Jekyll a bit to get a feel for how it actually works, and to get annoyed by it ;)

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2017-07-27 07:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (404 votes). Check out past polls.