我是一个 ruby 新手,他正试图读取这样的文本文件(Valgrind 模拟输出):
--------------------------------------------------------------------------------
Profile data file 'temp/gt_1024_2_16.out'
--------------------------------------------------------------------------------
I1 cache: 1024 B, 16 B, 2-way associative
D1 cache: 32768 B, 64 B, 8-way associative
LL cache: 3145728 B, 64 B, 12-way associative
Profiled target: bash run.sh
Events recorded: Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw
Events shown: Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw
Event sort order: Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw
Thresholds: 99 0 0 0 0 0 0 0 0
Include dirs:
User annotated:
Auto-annotation: off
--------------------------------------------------------------------------------
Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw
--------------------------------------------------------------------------------
1,894,017 246,981 2,448 519,124 4,691 2,792 337,817 1,846 1,672 PROGRAM TOTALS
// other data
我想提取 PROGRAM TOTALS 表并将其放入哈希中。就像是...
myHash = { :Ir => 1894017, :I1mr => 246981, ILmr => 2448, ..., DLmw => 1672 }
这样做的最佳选择是什么?CSV 课程可以帮助我吗?谢谢一堆。
我当前的代码:
file = File.open(fileName, "r")
while header = file.gets
if header =~ / Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw /
# Found the header
file.gets # skip the ---- line
values = file.gets
puts "Header: " + header
puts " Data: " + values
break
end
end
我有这个输出:
Header: Ir I1mr ILmr Dr D1mr DLmr Dw D1mw DLmw
Data: 1,894,017 246,981 2,448 519,124 4,691 2,792 337,817 1,846 1,672 PROGRAM TOTALS
我怎样才能将这两个字符串加入一个哈希?