使方括号不出现在新行上?

时间:2017-06-18 11:15:07

标签: perl sorting debugging text

我目前正在使用包含某些文件的数据点的.txt文件。 由于文件很大,它们是用较小的部分处理的,但是从处理过程中提取的输出没有按任何顺序排序。

它们存储如下:

#include<stdio.h>

void sort(int values[], int n)
{
int c,d,t=n;
//if we have n elements then outer loop must iterate for n times.
for(c=0;c<n;c++)
{
    /*after every complete iteration we get a
    sorted element in last position, next iteration
     will be one less, so we assigned value of n into t
     and we will decrease it by 1 after every internal complete iteration.
     */
     for(d=0;d<t-1;d++)
        {
          if(values[d]>values[d+1])
            {
               int temp;
               temp = values[d+1];
               values[d+1] = values[d];
               values[d] = temp;
            }
        }
    //decrease the test cases in next iteration,
    //we already have last element sorted.
    t--;
  }
}
int main()
{
  int i;
  int values[]={24,12,2,4,6};
  sort(values, 5);

   for(i=0; i<5; i++)
     {
        printf("%d\n",values[i]);
     }

return 0;
}

存储的格式为:1_1_0_1_0_1_1_0_232 [ 0 -19.72058 -18.89882 ] 1_0_0_0_0_0_0_0_0 [ -0.5940279 -1.949468 -1.185638 ] 1_0_1_1_0_1_1_1_100 [ -5.645662 -0.005585805 -6.196068 ] 1_0_1_1_0_1_1_1_101 [ -15.86037 -1.192093e-07 -18.77053 ] 1_0_1_1_0_1_1_1_102 [ -0.5648238 -1.970869 -1.230303 ] 1_0_1_1_1_0_1_0_103 [ -0.5750521 -1.946886 -1.222114 ] 1_0_1_1_1_0_1_0_104 [ -0.5926428 -1.941596 -1.191844 ] 1_0_1_1_1_0_1_0_105 [ -25.25665 0 -31.0921 ] 1_0_1_1_1_0_1_0_106 [ -0.001282441 -6.852591 -8.399776 ] 1_0_1_1_1_0_1_0_107 [ -0.0001649993 -8.857877 -10.69688 ] 1_0_1_1_1_0_1_0_108 [ -21.66693 0 -26.18516 ] 1_0_1_1_1_0_1_0_109 [ -5.444038 -0.004555213 -8.408965 ] 1_1_0_1_0_1_0_0_200 [ -4.023561 -0.01851013 -7.704897 ] 1_1_0_1_0_1_0_0_201 [ -0.443548 -3.057277 -1.167226 ] 1_1_0_1_0_1_0_0_202 [ -0.0001185011 -9.042104 -15.60585 ] 1_1_0_1_0_1_0_0_203 [ -5.960466e-07 -14.37778 -25.2224 ] 1_1_0_1_0_1_0_0_204 [ -0.5770675 -1.951139 -1.21623 ] 1_1_0_0_1_0_1_1_205 [ -0.5849463 -1.938798 -1.207353 ] 1_1_0_0_1_0_1_1_206 [ -0.5785673 -1.949474 -1.214192 ] 1_1_0_0_1_0_1_1_207 [ -27.21529 0 -32.21676 ] 1_1_0_0_1_0_1_1_208 [ -8.75938 -0.0001605878 -12.53627 ] 1_1_0_0_1_0_1_1_209 [ -1.281936 -0.3837854 -3.188763 ] 1_0_0_0_0_0_0_1_20 [ -0.2104172 -4.638866 -1.714325 ] 1_1_1_0_0_1_1_1_310 [ -11.71479 -9.298368e-06 -13.70222 ] 1_1_1_0_0_1_1_1_311 [ -24.71166 0 -30.45412 ] 1_1_1_0_0_1_1_1_312 [ -2.145031 -0.1357486 -4.617914 ] 1_1_1_0_0_1_1_1_313 [ -5.943637 -0.003112446 -7.630904 ] 1_1_1_0_0_1_1_1_314 [ 0 -25.82314 -31.98673 ] 1_1_1_0_0_1_1_1_315 [ -8.178092e-05 -13.60563 -9.426649 ] 1_1_1_0_0_1_1_1_316 [ -0.00326875 -6.071715 -6.952539 ] 1_1_1_0_0_1_1_1_317 [ -17.92782 0 -24.64391 ] 1_1_1_0_0_1_1_1_318 [ -2.979753 -0.05447901 -6.11194 ] 1_1_1_0_0_1_1_1_319 [ -0.7661145 -1.118131 -1.568804 ] 1_0_0_0_0_0_0_1_31 [ -0.5749408 -1.961912 -1.215127 ] 1_0_0_0_0_0_0_0_10 [ -4.64927e-05 -9.977531 -20.60117 ] 1_0_1_1_1_1_0_1_120 [ -0.4925551 -1.135103 -2.694917 ] 1_0_1_1_1_1_0_1_131 [ -0.6127387 -1.958336 -1.148721 ] 1_1_0_0_0_0_0_1_142 [ -0.008494892 -6.882521 -4.901772 ] 1_1_0_0_0_1_1_1_153 [ 0 -20.48085 -27.38916 ] 1_1_0_0_1_0_1_0_164 [ -0.5370184 -1.622399 -1.52286 ] 1_1_0_0_1_0_1_0_175 [ -24.08685 0 -29.42813 ] 1_1_0_0_1_1_1_0_186 [ -1.665665 -0.2307523 -4.074597 ] 1_0_0_0_0_0_0_0_1 [ -0.5880737 -1.945877 -1.198183 ] 1_1_0_0_1_0_1_1_210 [ -0.001396737 -6.574267 -21.30147 ] 1_1_0_1_0_1_1_0_221 [ -0.7456465 -1.893918 -0.980585 ] 1_0_0_0_0_0_1_1_42 [ -3.838613e-05 -10.23002 -13.01793 ] 1_0_0_0_0_0_1_1_43 [ -22.25132 0 -28.8467 ] 1_0_0_0_0_0_1_1_44 [ -6.688306 -0.001266626 -10.79875 ] 1_0_0_0_0_0_1_1_45 [ -0.429086 -2.197691 -1.436171 ] 1_0_0_0_0_0_1_1_46 [ -0.6683982 -1.928907 -1.072464 ] 1_0_0_0_1_0_0_1_47 [ -0.5767454 -1.972311 -1.206838 ] 1_0_0_0_1_0_0_1_48 [ -0.5789171 -1.965128 -1.206118 ] 1_0_0_0_1_0_0_1_49 [ -19.90514 0 -25.12686 ] 1_0_0_0_0_0_0_0_4 [ -4.768373e-07 -14.66496 -28.4888 ] 1_0_0_0_1_0_0_1_50 [ -0.01524216 -6.729354 -4.273614 ] 1_0_0_0_1_0_0_1_51 [ -3.576279e-07 -14.9054 -27.44406 ] 1_0_0_0_1_0_0_1_53 [ -0.003753785 -8.922103 -5.623135 ]

我目前使用perl脚本对数据点进行排序。

<name>_<part> [<data points>]

这会创建一个输出:

perl -n00e '
    while ( /([\d_]*)_(\d*) \s* \[ \s* (.*?) \s* \]/gmsx ) {
        ($name,$part,$datapoints) = ($1,$2,$3);
        $hash{$name}{$part}=$datapoints;
    }
    while (($key,$v)=each %hash) {
        print "$key [\n", (
            map "${$v}{$_}\n", sort {$a<=>$b} keys %{$v}
        ), "]\n";
    }
'

哪个是正确的,但是方括号不应该在新行上, 但是在打印完最后一个数据点后放置一个空格距离。 它看起来不像Perl脚本本身明确地创建一个新行 但是有些命令正在调用一个新行..是否有可能否定这种效果?

1 个答案:

答案 0 :(得分:-1)

这是@bytepusher在评论中推荐的内容:

while (($key,$v)=each %hash) {
        print "$key [\n", join("\n",
            map "${$v}{$_}", sort {$a<=>$b} keys %{$v}
        ), "             ]\n";
}