#!/usr/bin/env perl
# file: build_vector.pl
use strict;
use warnings;
my @vector; # the 2-d vector
my %mark; # mark the occurrence of the number in the first column
my $index = -1; # first dimensional index of the vector
while (<>) {
chomp;
my ($first, $second) = split /\s+/;
next if $second eq '';
if (not exists $mark{$first}) {
$mark{ $first } = ++$index;
push @{ $vector[$index] }, $first;
}
push @{ $vector[$index] }, $second;
}
# dump results
for my $i (0..$#vector) {
for my $j (0..$#{ $vector[$i] }) {
print "$vector[$i][$j] ";
}
print "\n";
}
このスクリプトは、スクリプトの出力を処理し、でベクトルを構築します@vector
。スクリプトにファイル名generator.pl
がある場合は、次のように呼び出すことができます。
$ perl generator.pl | perl build_vector.pl
アップデート:
use strict;
use warnings;
my $nodesNumber = 100 ;
my $communitiesNumber = 10;
my $prob_communities = 0.3;
my @vector; # the 2-d vector
my %mark; # mark the occurrence of the number in the first column
my $index = -1; # first dimensional index of the vector
for my $i (1 .. $nodesNumber){
for my $j (1 .. $communitiesNumber){
my $random_number=rand();
if ($prob_communities > $random_number){
if (not exists $mark{$i}) {
$mark{ $i } = ++$index;
push @{ $vector[$index] }, $i;
}
push @{ $vector[$index] }, $j;
}
}
}
# dump results
for my $i (0..$#vector) {
for my $j (0..$#{ $vector[$i] }) {
print "$vector[$i][$j] ";
}
print "\n";
}