in reply to Tallying appearance of a unique string from hash keys
We have a file with lines. Each line contains an ID followed by a tab followed by another ID. You want to know how many times each ID appears in the file, that's called the degree. And then you want to summarize how many times each degree appears.
If so then this should solve the problem:
If this is not the question you are asking, please clarify your question. Showing us a small example would be best. For example give us 5-20 lines of input and what output you'd expect from that.#! /local/bin/perl use strict; use warnings; my %degree; my $filename = "edges.txt"; open(my $fh, "<", $filename) or die "Can't open '$filename': $!"; while (<$fh>) { if (/(\S+)\t(\S+)/) { $degree{$1}++; $degree{$2}++; } } my %degree_distribution; $degree_distribution{$_}++ for values %degree; for my $id (sort keys %degree) { my $d = $degree{$id}; my $freq = $degree_distribution{$d}; print "$id has degree:\t$d\t(freq: $freq)\n"; }
|
|---|