Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Put multiple values as columns inside a hash

by spazm (Monk)
on Jun 18, 2010 at 04:18 UTC ( #845304=note: print w/replies, xml ) Need Help??

in reply to Put multiple values as columns inside a hash

  1. what does your data file look like?
  2. What does the middle element, $freq, represent?
  3. What is in the data file when the occurrence rate is zero? row skipped or zero elements?
  4. What do you intend to happen with your grep? You want keys where any month is greater than 10?
  5. What do you intend to happen with your sort? $multperc{$a} and {$b} are array references, so you're comparing on some nonsensical pointer value. This was probably sorting by value before you turned the value into an array ref. $multperc{$a}->[0] <=> $multperc{$b}->[0] will sort by the first month value.
Your code looks close to working, let's get it going.

Replies are listed 'Best First'.
Re^2: Put multiple values as columns inside a hash
by buzzybeewhee (Initiate) on Jun 18, 2010 at 05:58 UTC

    thanks for all the replies! :D

    To Almut:

    Actually, judging from what almut said...It may be kind of impossible to generate a script that is able to differentiate which columns have frequencies of 0...

    Because the input file is actually like that:

    L122Q 40 97.56097561 V100I 15 36.58536585 T373I 10 24.3902439 V217I 4 9.756097561 G16D 3 7.317073171 L283P 3 7.317073171 D21N 2 4.87804878 S450N 2 4.87804878 D53E 2 4.87804878 I109V 2 4.87804878 R236K 2 4.87804878 K351R 2 4.87804878 K470R 2 4.87804878 K400R 2 4.87804878 I61L 2 4.87804878 V425I 2 4.87804878 N309T 2 4.87804878 M105V 2 4.87804878 K452R 2 4.87804878 E372D 2 4.87804878 L456V 2 4.87804878 M191L 2 4.87804878 A190V 2 4.87804878 V444I 2 4.87804878 V313Y 2 4.87804878 T373A 2 4.87804878 M316I 2 4.87804878 S498N 2 4.87804878 R293K 2 4.87804878 N433T 2 4.87804878
    (this is only 1 little portion)

    The system won't really know where each month start or ends right? ): So it also won't know when the frequency for each is 0... ...How about if I use separate files instead? Maybe 1 file for each month? Would it be possible to achieve my goal this way?

    Sorry for the pestering and I really appreciate you pointing out the fatal error in my question! :))

    To Spazm:

    1. I have given a sample of the data file already, thanks for taking the effort to really help me look at my script...although what I'm asking for is a bit illogical ):

    2. $freq represents the middle number in my data file...I'm still a newbie at this, so I may actually have a lot of extra steps that do not contribute to anything, but I don't really know how to make them more concise

    3. When the occurrence rate is zero, it just doesn't appear in the data file...

    4. My grep is to get values larger than 10, because I'm only looking at keys which have an occurrence rate of above 10 for any months

    5. Thanks a lot for this helpful input! :D I was wondering why the sort function no longer worked after I changed it into an array. This is really useful! :)

    Many thanks for helping me look at my script, I hope it'll start working too! :))

    Best Regards,


      How about if I use separate files instead? Maybe 1 file for each month?

      Yes, if that's possible you could do something like this:

      my $mon_idx = 0; for my $fname ( qw(jan.dat feb.dat mar.dat apr.dat may.dat) ) { open my $fh, "<", $fname or die "Couldn't open '$fname': $!"; while (<$fh>) { my ($acc, $freq, $perc) = split /\t/; $mutperc{$acc}[$mon_idx] = $perc; } $mon_idx++; } for my $acc (sort keys %mutperc) { print $acc; for my $mon_idx (0..4) { my $perc = $mutperc{$acc}[$mon_idx]; $perc = 0 unless defined $perc; print "\t$perc"; } print "\n"; }

      By usnig a fix index ($mon_idx) instead of pushing onto the arrays, the values would end up in the right place and you'd get undef for the months with missing values, which you can later turn into 0.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://845304]
[marto]: Hi Dr. 1nickt

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2017-12-18 12:00 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (485 votes). Check out past polls.