如果你对代码有任何疑问,我会说:是的,我可以回答,但这并不是 Perl 代码的好例子。只是快写。
my $previous_count = "-1"; #beginning, we will think, that no spaces.
my $current_count = "0"; #current default value
my $maximum_count = 3; #u say so
my $to_written = "";
my $delimiter_between_columns = ",";
my $newline_separator = ";";
my $symbol_at_the_beginning = "#"; #input any symbol. But I suppose, you want "\s" <- whitespace' symbol class. input it like this: $var = "\s";
my @aggregate_array_of_ports=();
while(my $row = <DATA>){
#ok, read.
chomp($row);
#print "row is : $row\n";
if($row =~ m/^([$symbol_at_the_beginning]*)/){
#print length($1);
$current_count = length($1) / 2; #take number of spaces divided by 2
$row =~ s/^[$symbol_at_the_beginning]+//;
#hint here, we can get counts as 0,1,2,3 <-see?
#if you take first and third word, you need to add 2 separators.
#OR if you take count with LESSER then previous count, it mean, that you need output
#print"prev : $previous_count and curr : $current_count\n ";
#print"I will write: $to_written\n";
#print "\n PREV: $previous_count --> CURR: $current_count \n";
if($previous_count>=$current_count){
#output here
print "$to_written".$newline_separator."\n";
$previous_count = 0;
$to_written = "";
}
$previous_count = 0 if($previous_count==-1);
#print "$delimiter_between_columns x($current_count-$previous_count)\n";
#print "current: $current_count previous: $previous_count \n";
$to_written .= $delimiter_between_columns x ($current_count - $previous_count + (($current_count-$previous_count)==3?2:0) )."$row";
if ($current_count==($maximum_count-1)){
#print "I input this!: $to_written\n";
$to_written = prepare_to_input_four_spaces($to_written, $delimiter_between_columns);
}
$previous_count = $current_count;
#print"\n";
}
}
#print "$to_written".$newline_separator."\n";
sub prepare_to_input_four_spaces{
my $str = shift; #take string
my $delim = shift;
if ($str=~ m/(.+?[>])\s+(\d+)[:]\s+(.+?)$/){
#here I want to find first capture group before [>] (also it includes) |(.+?[>])|
#next, some spaces |\s+| and I want to catch port |(\d+)|.
#next, |[:]| symbol and some spaces again |\s+| before the tail of the string.
#and will catch this tail: |(.+?)$|.
#where $ mean the right "border" of the string (really - end of the string)
$str = $1.$delim.$2.$delim.$3;
}
return $str;
}
=pod
__DATA__
ABC
EFG"123"
HIJK (12345)
LMNOP "Test"
sssstt"123"
QRS
TU"223"
vwx"55"
www"88"
yz:test1
__END__
=cut
__DATA__
ABC
##EFG"123"
####<HIJK> 22: test file
######LMNOP "Test"
######sssstt"123"
QRS
##TU"223"
####<www> 32: test2 file
######yz test1
####<www> 88: test3 file
######rreeeww
######oooiiiii
##PP
##ss
####<qqq> 89: test6 file
######hhhhggg
######bbbbaaa
######cccczzz
######uu test3