in reply to Spliting file + removing column
Things to note.#!/usr/bin/perl -w use strict; # This should really be passed in on the command line or something. my $data_file = "HIVgag.ct"; # Find the vertices. my @vertices; open(my $fh, "<", $data_file) or die "Can't open '$data_file': $!"; while (<$fh>) { if (/energy/i) { if (not @vertices) { # This is the first line of the first block. Do nothing. next; } else { # We have completed the first block. last; } } my @row = split /\s+/, $_; push @vertices, $row[0]; } # Scalar context turns @vertices into the number of elements it has. print "*vertices " . @vertices . "\n"; for my $vertex (@vertices) { print "$vertex G\n"; } print "*edges\n"; seek($fh, 0, 0); my %connect; my $position = @vertices - 1; while (<$fh>) { $position++; if (/energy/i) { if ($position != @vertices) { die "In line $., too few vertices found"; } $position = -1; next; } my ($this_vertex, $type, @row) = split /\s+/, $_; if ($this_vertex ne $vertices[$position]) { die "Unexpected vertex '$this_vertex' at line $."; } for my $other_vertex (@row) { if (0 == $other_vertex or $this_vertex == $other_vertex) { next; } my $key = "$this_vertex $other_vertex"; $connect{$key}++; print "$key $connect{$key}\n"; } }
for my $vertex_pair (sort keys %multiplicity) { print "$vertex_pair $multiplicity{$vertex_pair}\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Spliting file + removing column
by AG87 (Acolyte) on Jan 13, 2011 at 17:24 UTC | |
by tilly (Archbishop) on Jan 13, 2011 at 17:42 UTC | |
by AG87 (Acolyte) on Jan 13, 2011 at 17:59 UTC | |
by tilly (Archbishop) on Jan 13, 2011 at 18:20 UTC |