Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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
Ruby 从文件中提取列_Ruby_File Io - Fatal编程技术网

Ruby 从文件中提取列

Ruby 从文件中提取列,ruby,file-io,Ruby,File Io,我是Ruby的新程序员。我试图从一个文件中提取两列并执行一些操作。我想我可以使用regexp来实现这个目的?这是我的档案: MMU June 2002 Dy MxT MnT AvT HDDay AvDP 1HrP TPcpn WxType PDir AvSp Dir MxS SkyC MxR MnR AvSLP 1 88 59 74 53.8 0.00 F 280 9.6 270 17 1.6 93 2

我是Ruby的新程序员。我试图从一个文件中提取两列并执行一些操作。我想我可以使用regexp来实现这个目的?这是我的档案:

 MMU June 2002

  Dy MxT   MnT   AvT   HDDay  AvDP 1HrP TPcpn WxType PDir AvSp Dir MxS SkyC MxR MnR AvSLP

   1  88    59    74          53.8       0.00 F       280  9.6 270  17  1.6  93 23 1004.5
   2  79    63    71          46.5       0.00         330  8.7 340  23  3.3  70 28 1004.5
   3  77    55    66          39.6       0.00         350  5.0 350   9  2.8  59 24 1016.8
   4  77    59    68          51.1       0.00         110  9.1 130  12  8.6  62 40 1021.1
   5  90    66    78          68.3       0.00 TFH     220  8.3 260  12  6.9  84 55 1014.4
   6  81    61    71          63.7       0.00 RFH     030  6.2 030  13  9.7  93 60 1012.7
   7  73    57    65          53.0       0.00 RF      050  9.5 050  17  5.3  90 48 1021.8
   8  75    54    65          50.0       0.00 FH      160  4.2 150  10  2.6  93 41 1026.3
   9  86    32*   59       6  61.5       0.00         240  7.6 220  12  6.0  78 46 1018.6
这就是我想做的:

 result_arr = []
 File.open("filename") do |f|
    while (line = f.gets)
      ary = line.split
      day = ary[0]
      max = ary[1]
      min = ary[2]
      result = max.to_i - min.to_i
     result_arr << result unless result == 0
   end
   puts result_arr.min
 end
 ~    
基本上我想打印第一列和minMxt-Mnt的结果 我正在使用一个数组,但我正在尝试使用一个二维数组,可能不知道如何做到这一点


非常感谢。

以下内容如何:

File.open("file.txt") do |f|
  while (line = f.gets)
    ary = line.split
    do_whatever_you_want_to(ary)
  end
end

Stringsplit将把你的行变成一个数组,在空格和制表符处断开。

阅读你对第一个答案的评论,我想这就是你想要做的:

results = [] #array to store the difference between the "Mxt" and "Mnt" fields

File.open("/path/to/file").each do |line|
  # skip header rows (there seem to be multiple in the file)
  # you might need to adjust the regex that checks for the presence of a data row
  # here I'm assuming that data rows start with a digit
  if line =~ /^\d/
    arr = line.split
    # you can use the column indices to get to the data you want
    results << arr[1] - arr[2]  
  end
end

通常,您希望像这样“解包”固定宽度数据:

'   1  88    59 '.unpack('A6A6A6')

我删除了java标记,因为你说你是Ruby程序员。@Andrea:这就是我要做的:result_arr=[]File.openw_data.dat do | f |而line=f.gets ary=line.split day=ary[0]max=ary[1]min=ary[2]结果=最大到-最小到结果。我必须测试正则表达式才能确定。不过,我尽量避免在像split这样简单的函数可以使用的代码中使用正则表达式,因为我发现它使代码更可读,更不容易出现错误。