open(IN,"nexus.html")|| die "no file named \"nexus.html\" found in this directory\n";
open(OUT,"> nexus.net")|| die "could not open output file, make sure you have write permissions\n";;

while($line=<IN>) {
	chomp;
	if ($line =~ /^totalFriends=(\d+)/) {
		$totalFriends = $1;
		print "number of friends = $totalFriends\n";
	}
	if ($line =~ /^linkCount=(\d+)/) {
		$linkCount = $1;
		print "number of connections between friends = $linkCount\n";
	}
	if ($line =~ /^linksTo=/) {
		$line =~ s/:\d+}};$//;
		$line =~ s/:\d+//g;
		@ar = split(/},/,$line);
		for $are (@ar) {
			$are =~ s/linksTo={//;
			($from,$tolist) = split(/:{/,$are);
			@tos = split(/,/,$tolist);
			for $to (@tos) {
				$link{$from}{$to} = 1;
			}
		}
	}
	if ($line =~ /^nodeData=/) {
		$line =~ s/:\d+]};$//;
		@ar = split(/],/,$line);
		for $are (@ar) {
			$are =~ s/nodeData={//;
			if ($are =~ /^(\d+):/) {$id = $1;} else {print "error\n";}
			if ($are =~ /\"([^"]+)/) {
				$name = $1;
				$name{$id} = $1;
			}
		}
	}
}

$i = 1;
print OUT "*Vertices $totalFriends\r\n";
for $friend (keys %name) {
	print OUT "  $i  \"$name{$friend}\"\r\n";
	$netid{$friend} = $i;
	$i++;
}

print OUT "*Edges\r\n";
for $from (keys %link) {
	for $to (keys %{$link{$from}}) {
		next if ($printed{$to}{$from} == 1);
		print OUT "$netid{$from} $netid{$to} 1\r\n";
		$printed{$from}{$to} = 1;
	}
}
	