Skip to content

Instantly share code, notes, and snippets.

@phonyphonecall
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save phonyphonecall/cdbe75255d863ffe0c25 to your computer and use it in GitHub Desktop.

Select an option

Save phonyphonecall/cdbe75255d863ffe0c25 to your computer and use it in GitHub Desktop.

Revisions

  1. phonyphonecall revised this gist Feb 23, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion hw1_parta1.pl
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@
    my @binct = (0, 0, 0, 0, 0);


    $count = 0;
    $count = -1;
    #iterate over each line to count bins (and total)
    while(<$fd>) {
    $count++;
  2. phonyphonecall renamed this gist Feb 21, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. phonyphonecall created this gist Feb 21, 2015.
    70 changes: 70 additions & 0 deletions hw1_part1.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    #!/usr/bin/perl
    use strict;
    use warnings;

    #get file in
    open(my $fd, "<", $ARGV[0]) or die "cannot open file $!";

    #iterate over each line to find max
    my $max = -1;
    my $count = -1;
    while(<$fd>) {
    $count++;
    next if $count == 0;
    my @line = split("\t", $_);

    if ($max < $line[1]) {
    $max = $line[1];
    }
    }

    print "max temp: $max\n";

    seek($fd, 0, 0);

    my @binct = (0, 0, 0, 0, 0);


    $count = 0;
    #iterate over each line to count bins (and total)
    while(<$fd>) {
    $count++;
    next if $count == 0;

    my @line = split("\t", $_);

    no warnings 'numeric';
    my $temp = $line[1] * 1.0000000;

    if($temp > 0.1 * $max and $temp <= $max) {
    $binct[0]++;
    } elsif($temp > 0.01 * $max and $temp <= 0.1 * $max) {
    $binct[1]++;
    } elsif($temp > 0.001 * $max and $temp <= 0.01 * $max) {
    $binct[2]++;
    } elsif($temp > 0.0001 * $max and $temp <= 0.001 * $max) {
    $binct[3]++;
    } elsif($temp <= 0.0001 * $max) {
    $binct[4]++;
    } else {
    warn "bad temp: $temp\n";
    }
    }

    close($fd);

    #convert to percentage
    $binct[0] = ($binct[0] / $count) * 100;
    $binct[1] = ($binct[1] / $count) * 100;
    $binct[2] = ($binct[2] / $count) * 100;
    $binct[3] = ($binct[3] / $count) * 100;
    $binct[4] = ($binct[4] / $count) * 100;

    print "bin0: $binct[0]%\n";
    print "bin1: $binct[1]%\n";
    print "bin2: $binct[2]%\n";
    print "bin3: $binct[3]%\n";
    print "bin4: $binct[4]%\n";


    print "done\n";