Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 在第一个数组位置按字母顺序对整个单词排序_Perl - Fatal编程技术网

Perl 在第一个数组位置按字母顺序对整个单词排序

Perl 在第一个数组位置按字母顺序对整个单词排序,perl,Perl,我有一个文件,其中的值由管道分隔| 输入: 我试图按字母顺序对第一列进行排序,并写出排序后的文件 结果: 我所尝试的: 可以对第一个数组位置进行排序,但不能写出所有文件内容 $filename = 'test.dat'; open (INPUT,"$filename"); open (OUTPUT,">out.dat"); while (<INPUT>) { @array = split('\|'); @arr =

我有一个文件,其中的值由管道分隔
|

输入: 我试图按字母顺序对第一列进行排序,并写出排序后的文件

结果: 我所尝试的: 可以对第一个数组位置进行排序,但不能写出所有文件内容

 $filename = 'test.dat';
 open (INPUT,"$filename");
 open (OUTPUT,">out.dat");

 while (<INPUT>)
 {
   @array = split('\|');
   @arr = split(' ',$array[0]);
   $,=" ";
   print OUTPUT sort @arr,"\n";
  }
   close (INPUT);
   close (OUTPUT);
$filename='test.dat';
打开(输入“$filename”);
打开(输出“>out.dat”);
而()
{
@数组=拆分(“\\\”);
@arr=拆分(“”,$array[0]);
$,=" ";
打印输出排序@arr,“\n”;
}
关闭(输入);
关闭(输出);
对每行中的所有内容进行排序

 $filename = 'test.dat';
 open (INPUT,"$filename");
 open (OUTPUT,">out.dat");

 while (<INPUT>)
 {
   @arr = split(' ');
   $,=" ";
   print OUTPUT sort @arr,"\n";
  }
   close (INPUT);
   close (OUTPUT);
$filename='test.dat';
打开(输入“$filename”);
打开(输出“>out.dat”);
而()
{
@arr=分割(“”);
$,=" ";
打印输出排序@arr,“\n”;
}
关闭(输入);
关闭(输出);

行拆分成数组后

my @ary = split '\|';
您需要
split
第一个元素,
sort
列表,然后
join
返回

my $first = join ' ', sort split ' ', shift @ary;
其中
shift
删除(并返回)第一个元素

然后重新构建字符串

my $new_string = join '|', $first, @ary;
这可以在没有临时变量的情况下完成。最后两个步骤可以在一条语句中完成,所有步骤都可以在
print
语句中完成

评论

  • 请始终使用
    使用警告启动程序
    使用严格

  • 使用三个参数形式的
    open

    open my $fh, '<', $file or die "Can't open $file: $!";
    
    这将捕获第一个字段(直到管道),并在其上运行上述代码,这得益于
    /e
    修改器,该修改器使替换零件作为代码进行评估

    我们还需要匹配管道,然后把它放回去。这可以通过使用


    它只声明
    (?=…)
    中的模式在那里,而不使用它。

    行拆分成数组后

    my @ary = split '\|';
    
    您需要
    split
    第一个元素,
    sort
    列表,然后
    join
    返回

    my $first = join ' ', sort split ' ', shift @ary;
    
    其中
    shift
    删除(并返回)第一个元素

    然后重新构建字符串

    my $new_string = join '|', $first, @ary;
    
    这可以在没有临时变量的情况下完成。最后两个步骤可以在一条语句中完成,所有步骤都可以在
    print
    语句中完成

    评论

    • 请始终使用
      使用警告启动程序
      使用严格

    • 使用三个参数形式的
      open

      open my $fh, '<', $file or die "Can't open $file: $!";
      
      这将捕获第一个字段(直到管道),并在其上运行上述代码,这得益于
      /e
      修改器,该修改器使替换零件作为代码进行评估

      我们还需要匹配管道,然后把它放回去。这可以通过使用


      它只声明
      (?=…)
      中的模式在那里,而不使用它。

      您的第一位代码非常接近正确。不过,您只输出第一列-您需要打印出
      @array
      的内容。如果用排序后的结果替换
      @array
      的第一个元素,则可以这样写出来

      @array = split('\|');
      $array[0] = join(" ",sort split(' ',$array[0]));
      print OUTPUT join("|",@array),"\n";
      

      您的第一段代码非常接近正确。不过,您只输出第一列-您需要打印出
      @array
      的内容。如果用排序后的结果替换
      @array
      的第一个元素,则可以这样写出来

      @array = split('\|');
      $array[0] = join(" ",sort split(' ',$array[0]));
      print OUTPUT join("|",@array),"\n";
      

      如果您不再在代码中硬编码文件名,那么您的程序将更加灵活。最好尽可能从
      STDIN
      读取数据,然后写入
      STDOUT
      。这就是代码的作用

      #!/usr/bin/perl
      
      use strict;
      use warnings;
      
      while (<>) {
        # Split the record into the first field (the one we want to sort)
        # and everything else.
        my ($first, $rest) = split /\|/, $_, 2;
      
        # Split the first record into words
        my @words = split /\s+/, $first;
      
        # Sort @words and then join the line back together and print it.
        print join(' ', sort @words), "|$rest";
      }
      

      我们避免了打开任何文件句柄(操作系统为我们做的),而且如果文件名发生变化,我们也不需要更改程序。

      如果您不再在代码中硬编码文件名,您的程序将更加灵活。最好尽可能从
      STDIN
      读取数据,然后写入
      STDOUT
      。这就是代码的作用

      #!/usr/bin/perl
      
      use strict;
      use warnings;
      
      while (<>) {
        # Split the record into the first field (the one we want to sort)
        # and everything else.
        my ($first, $rest) = split /\|/, $_, 2;
      
        # Split the first record into words
        my @words = split /\s+/, $first;
      
        # Sort @words and then join the line back together and print it.
        print join(' ', sort @words), "|$rest";
      }
      
      perl -pe 's/^([^|]+)/@a = sort split ' ', $1; "@a";/e' myfile
      

      我们不必打开任何文件句柄(操作系统为我们做了这件事),而且如果文件名发生变化,我们也不需要更改程序。

      Tyexplanation@SinanÜnür谢谢你,需要提出一个非常好的观点。一步一步添加为bullet.Tyexplanation@SinanÜnür谢谢你,需要提出一个非常好的观点。添加为一个项目符号。看一看
      perl -pe 's/^([^|]+)/@a = sort split ' ', $1; "@a";/e' myfile