ruby - 解析 bpftrace 输出数据
问题描述
我正在尝试使用 ruby 解析 bpftrace 文件的以下输出。我考虑过拆分'|'
,然后我需要从中获取值"[4, 8) 824"
。需要将这两个值放入一个数组中。我也在考虑使用 trim 方法,但肯定有更好的方法 - 也许使用正则表达式。请给我一些关于如何进行的指导,好吗?
输入:[4, 8) 824 |@@@@ |
first_array = []
text=File.foreach('/.../test.txt').with_index do |line|
puts "#{line}"
values=line.split("|")
first_array=values[0].split(" ")
puts first_array
解决方案
您不需要按'|'
开箱即用的非数字拆分,拆分:
input = '[4, 8) 824 |@@@@ |'
input.split(/\D+/).reject(&:empty?).map(&:to_i)
#⇒ [4, 8, 824]
或者,正如 Cary 在评论中所建议的:
input.scan(/\d+/).map(&:to_i)
#⇒ [4, 8, 824]
推荐阅读
- asp.net - 如何将我的 ASP.NET 应用程序与 Epicor ERP 连接?
- matplotlib - 根据 x 值在 axhline 中指定 xmax
- php - 使用 xampp 访问 localhost 上的 html/php 文件时出现错误 404
- r - 从 R 中的 dlmSvd2var 函数(dlm 包)获得的 mse 矩阵是什么?
- flutter - 未来建造者状态
- >#a0d90):I/flutter(20935):RangeError(索引):无效值:有效值范围为空:0
- java - 使用 Mockito 模拟抽象类中的静态方法
- vhdl - VHDL 端口外部名称和过程模式
- javascript - react-native modal 官方示例不适用于 react-native-web,我该如何解决?
- django - Django计算ORM中行之间的差异
- php - Laravel - 将 RAW 查询转换为 Eloquent 查询(更新查询)