对输入文件的内容进行排序并写入输出文件 [英] Sort contents of input file and writing output file

查看:65
本文介绍了对输入文件的内容进行排序并写入输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对文本文件的内容进行排序,然后将其另存为另一个文本文件(或覆盖前一个文件).我对perl不太满意,因此似乎无法弄清楚为什么此代码段不起作用.

I want to sort the contents of a text file and save it as another text file (or overwrite the previous one). I'm not particularly good with perl so I can't seem to figure out why this code snippet does not work.

open(my $file, '>', $filename) or die $!;
print $file $string;

my @curOrd = qw( USD AUD BRL GBP CAD CNY DKK HKD INR IDR ILS JPY MXN NOK 
PHP PLN SGD SKK ZAR KRW SEK CHF TWD THB EUR MYR NZD SAR TRY RUB CZK AED CLP 
EGP MAD NGN OMR QAR );

my $curKnt = scalar @curOrd;
my $outfile = 'file1.txt';
my $infile = 'file2.txt';
open (OUTFILE, ">$outfile");

   foreach my $i (0..$curKnt) {
   open (INFILE, $infile);
   while(<INFILE>)
   {
        my @x= split(',', $_);
        print "x2 = $x[2]\n";
        print "cur_ord = $curOrd[$i]\n";
        if ($x[2] eq $curOrd[$i])  {
           print OUTFILE "$_";
        }
       @x=();
   }  # end of while

   close (INFILE);   

}  # end of foreach

close (OUTFILE);

该代码应该在保持相同格式的同时对每一行进行排序.但是,当我运行它时,我没有收到任何错误,但是找不到输出文件.

The code is supposed to sort each row while keeping the same format. When I run it, however, I get no errors but the output file is nowhere to be found.

输入文件:

20181231,USD,AED,3.6736
20181231,USD,AUD,1.4179
20181231,USD,BRL,3.8817
20181231,USD,CAD,1.3632
20181231,USD,CHF,0.9842
20181231,USD,CLP,694.7432
20181231,USD,CNY,6.8787
20181231,USD,CZK,22.4985
20181231,USD,DKK,6.5252
20181231,USD,EGP,17.9426
20181231,USD,EUR,0.8738
20181231,USD,GBP,0.7853
20181231,USD,HKD,7.8322
20181231,USD,IDR,14483.0392
20181231,USD,ILS,3.7554
20181231,USD,INR,69.5662
20181231,USD,JPY,110.0258
20181231,USD,KRW,1114.7559
20181231,USD,MAD,9.6044
20181231,USD,MXN,19.6584
20181231,USD,MYR,4.1383
20181231,USD,NGN,365.4984
20181231,USD,NOK,8.6848
20181231,USD,NZD,1.4902
20181231,USD,OMR,0.3858
20181231,USD,PHP,52.5907
20181231,USD,PLN,3.7581
20181231,USD,QAR,3.6734
20181231,USD,RUB,69.5418
20181231,USD,SAR,3.7533
20181231,USD,SEK,8.9336
20181231,USD,SGD,1.3637
20181231,USD,SKK,26.3251
20181231,USD,THB,32.4579
20181231,USD,TRY,5.2938
20181231,USD,TWD,30.6034
20181231,USD,USD,1.0000
20181231,USD,ZAR,14.4104

输出文件:

20181231,USD,USD,1.0000
20181231,USD,AUD,1.4220
20181231,USD,BRL,3.8828
20181231,USD,GBP,0.7880
20181231,USD,CAD,1.3643
20181231,USD,CNY,6.8782
20181231,USD,DKK,6.5295
20181231,USD,HKD,7.8316
20181231,USD,INR,69.7304
20181231,USD,IDR,14565.1115
20181231,USD,ILS,3.7693
20181231,USD,JPY,110.3364
20181231,USD,MXN,19.6687
20181231,USD,NOK,8.7288
20181231,USD,PHP,52.7242
20181231,USD,PLN,3.7660
20181231,USD,SGD,1.3674
20181231,USD,SKK,26.3387
20181231,USD,ZAR,14.4448
20181231,USD,KRW,1117.0950
20181231,USD,SEK,8.9869
20181231,USD,CHF,0.9846
20181231,USD,TWD,30.6213
20181231,USD,THB,32.6870
20181231,USD,EUR,0.8743
20181231,USD,MYR,4.1555
20181231,USD,NZD,1.4927
20181231,USD,SAR,3.7565
20181231,USD,TRY,5.2892
20181231,USD,RUB,69.4108
20181231,USD,CZK,22.5409
20181231,USD,AED,3.6728
20181231,USD,CLP,694.7031
20181231,USD,EGP,17.9262
20181231,USD,MAD,9.5636
20181231,USD,NGN,365.4074
20181231,USD,OMR,0.3860
20181231,USD,QAR,3.6423

推荐答案

这在我测试时按预期工作.我只能建议存在一些本地问题,这些问题会阻止您打开和写入输出文件.如果您检查了open()的返回值并采取了适当的措施,就会发现问题出在哪里.

This works as expected when I test it. I can only suggest that there are some local problems that prevent you from either opening and writing to the output file. You'd see what that problem is if you checked the return value from open() and took appropriate action.

我已经采取了收紧一些代码的自由:

I've taken the liberty of tightening up some of your code:

my @curOrd = qw( USD AUD BRL GBP CAD CNY DKK HKD INR IDR ILS JPY MXN NOK
                 PHP PLN SGD SKK ZAR KRW SEK CHF TWD THB EUR MYR NZD SAR
                 TRY RUB CZK AED CLP EGP MAD NGN OMR QAR );

my $outfile = 'file1.txt';
my $infile = 'file2.txt';
open (my $out_fh, '>', $outfile)
  or die "Cannot open $outfile: $!";

# Iterating over the values in a list is usually
# better than iterating over the indexes.
foreach my $cur (@curOrd) {
  open ($in_fh, '<', $infile)
    or die "Cannot open $infile: $!";

  while(<$in_fh>) {
    my @x = split(/,/);
    print "x2 = $x[2]\n";
    print "cur_ord = $cur\n";

    if ($x[2] eq $cur)  {
      print $out_fh $_;
    }
  }  # end of while

  close ($in_fh);
}  # end of foreach

close ($out_fh);

更新:多次打开和关闭输入文件的效率非常低.此版本将其打开一次,将数据拆分为二维数组,然后对其进行排序.

Update: Opening and closing your input file so many times is very inefficient. This version opens it once, splits the data into a two-dimensional array and then sorts that.

my @curOrd = qw( USD AUD BRL GBP CAD CNY DKK HKD INR IDR ILS JPY MXN NOK
                 PHP PLN SGD SKK ZAR KRW SEK CHF TWD THB EUR MYR NZD SAR 
                 TRY RUB CZK AED CLP EGP MAD NGN OMR QAR );

# Build a look-up table mapping currencies to their
# sort position
my $i = 0;
my %cur_lookup = map { $_ => $i++ } @curOrd;

my $outfile = 'file1.txt';
my $infile = 'file2.txt';
open (my $out_fh, '>', $outfile)
  or die "Cannot open $outfile: $!";

open(my $in_fh, '<', $infile)
  or die "Cannot open $infile: $!";

my @in_data = map { [ split /,/ ] } <$in_fh>;

print $out_fh
  map { join ',', @$_ }
  sort { $cur_lookup{$a->[2]} <=> $cur_lookup{$b->[2]} } @in_data;

close ($in_fh);
close ($out_fh);

这篇关于对输入文件的内容进行排序并写入输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆