#!/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
. 如果您的脚本有 filename 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";
}