Hello, So I've progressed in my script (for a newbie), but still have some gaps that I could use some help on if anyone is able to help.
#! /local/bin/perl use strict; use warnings; #Declare hash to pull IDs and corresponding degrees into from file my %degree; my %newdegree; my @geneID_1; my @geneID_2; my $filename = "edges.txt"; #Set a variable for our file name open(my $fh, "<", $filename) or die "Can't open file $filename."; #Op +en the file edges.txt while (<$fh>) { if ($_ =~ m/(\S+)\t(\S+)/) { #Match the IDs in the file to + $1 and $2 $degree{$1}++; #Count the appearance of each + ID, and store this as $degree{$2}++; #the value for that key (this + will be the degree) push (@geneID_1, $1); push (@geneID_2, $2); }} close $fh; #Close the file edges.txt #Calling the following subroutines DegreeDistribution(); RandomSequence(); DegreeRan(); #Subroutines can be found below sub DegreeDistribution{ #Determines the degree distribution (i.e. fre +quency of each degree) my %degree_distribution; $degree_distribution{$_}++ for values %degree; #Creates a hash wi +th the keys as the degrees and the values as the frequencies for my $id (keys %degree) { #This corresponds each b +elow to it's key my $d = $degree{$id}; #This is the degree value +corresponding to its gene ID my $freq = $degree_distribution{$d}; #This is the frequency val +ue corresponding to its gene ID??? print "$id has degree:\t$d\t(freq: $freq)\n"; }} sub RandomSequence{ #Generates a hash of random ID interactions and + each IDs degree my $length = scalar (@geneID_1); for (my $i=0; $i<$length; $i++){ my $ID = int(rand($length)); my $new_id1 = $geneID_1[$ID]; my $new_id2 = $geneID_2[$ID]; $newdegree{$new_id1}++; $newdegree{$new_id2}++; }} sub DegreeRan{ #same as sub DegreeDistribution but for the random +sequence my %degree_distribution; $degree_distribution{$_}++ for values %newdegree; #Creates a hash + with the keys as the degrees and the values as the frequencies for my $id (keys %degree) { #This corresponds each b +elow to it's key my $d = $degree{$id}; #This is the degree value +corresponding to its gene ID my $freq = $degree_distribution{$d}; #This is the frequency val +ue corresponding to its gene ID??? print "$id has degree:\t$d\t(freq: $freq)\n"; }} exit;
I have a few concerns: 1. I want to create three random networks, not just one. I'm not sure how to make my subroutine so that it produces a different hash each time (i.e. named differently), than I can return to the main script -- the return function didn't seem to work properly, it only returns one key-value pair not the whole list. 2. Similar to one, I want to run each hash through the DegreeDistribution subroutine, because making a separate subroutine for each hash defeats the purpose of a subroutine. 3. Probably also very related, I want to return the final values to the program so I can use them all (from the original dataset and the three random datasets) in an excel file. Thank you to anyone that responds.

In reply to Re^2: Tallying appearance of a unique string from hash keys by jack_j
in thread Tallying appearance of a unique string from hash keys by jack_j

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.