首先让我道歉,我是一名网络工程师,而不是编码员......所以请耐心等待。
这就是我面临的问题,我无法为我的生活找到一种优雅的方式来做到这一点。
我正在使用 nagios(相信你们中的许多人都熟悉它)并且正在从服务检查中获取性能数据。这一个特别返回如下值: 模块 2 入口温度 模块 2 导出温度 模块 2 asic-4 温度 模块 3 入口温度 模块 3 导出温度 模块 4 入口温度 模块 4 导出温度 ... 等等 这些值都显示在一个数组中。我想做的是: 匹配字符串中的前 2 个单词/值,以创建用于生成 RRD 图的数组键值“组”... RRD 部分我不需要任何帮助,但匹配和输出我做。
我还应该注意,这里也可能有不同的数组值,具体取决于数据来自的设备(即它可能显示为“Switch #1 Sensor #1 Temperature”),而我不是目前担心这一点,我将在未来使用这个脚本来评估这些值,以创建它们各自的图表。
所以,言归正传,我的想法是从原始数组创建两个数组: 最初使用 preg_match 来寻找/.outlet.|.asic./因为这些是“热”温度,然后通过将新数组分解为第二个值(int)或前两个值(模块#)供以后比较
其次使用 preg_match 寻找/.inlet./因为这些是“冷”温度,然后通过像前者一样分解新数组来进一步优化。
现在应该有两个带有 key=># 或 key=>module # 的数组 然后使用 array_intersect 在两个数组中查找匹配项并输出键,以便我可以使用它们生成图形。
这有意义吗?换句话说,我只想选择匹配的模块 # 条目以在我的图形中使用。即模块 2 入口、模块 2 导出、模块 2 asic...然后重复 - 模块 3 入口、模块 3 导出等...
这是我尝试过的方法,但根本没有按照我想要的方式工作:
$test = array("module 1 inlet temperature", "module 2 inlet temperature", "module 2 asic-4 temperature", "module 2 outlet temperature", "module 1 outlet temperature");
$results = array();
foreach($test as $key => $value) {
preg_match("/.*inlet.*|.*asic.*/", $test[$key]);
preg_match("/module [0-9]?[0-9]/", $test[$key]);
$results[] = $value;
}
if(preg_match("/.*outlet.*/", $test[$key]));
foreach($test as $key1 => $value1) {
preg_match("/module [0-9]?[0-9]/", $test[$key1]);
$results1[] = $value1;
}#
}
$results3 = array_intersect($results, $results1)
如有任何帮助,我们将不胜感激。我敢肯定我在这里的解释很困惑所以希望有人可怜我并帮助一个人...
提前致谢。
最佳答案
有点难以理解您的问题,但我想您正在寻找这样的结果?
$temps['module 1']['inlet'] = 20;
$temps['module 1']['outlet'] = 30;
$temps['module 2']['inlet'] = 25;
$temps['module 2']['outlet'] = 35;
$temps['module 2']['asic-4'] = 50;
然后您将使用这些数组生成图表?
只要你在一个数组中有标签,在另一个数组中有临时值,并且每个数组中标签和临时值的顺序是相同的......那么你将如何做到这一点:
// Split Names into Groups
$temps = array(20,25,50,35,30);
$labels = array("module 1 inlet temperature", "module 2 inlet temperature", "module 2 asic-4 temperature", "module 2 outlet temperature", "module 1 outlet temperature");
// Combine Lables to Values (Labels and Values must be in the same positions)
$data = array_combine($labels, $temps);
$temps = array();
foreach ($data as $label => $temp) {
$words = preg_split('/\s/i', $label);
// Combine first two pieces of label for component name
$component = $words[0] . ' ' . $words[1];
// Sensor name is on it's own
$sensor = $words[2];
// Save Results
$temps[$component][$sensor] = $temp;
}
// Print out results for debug purposes
echo '<pre>';
var_dump($temps);
echo '</pre>';
exit();
一旦你有了 $temp 数组,你就可以使用 foreach 循环遍历每个模块和传感器并打印出图表的值,或者只显示某些模块,或某些传感器等。
即使它不是您真正想要的,希望它能给您一些想法,您可以对其进行调整以适应。
关于php - "first 2 words"数组中值的多重匹配然后array_intersect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14208326/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
通过rubykoans.com,我在about_array_assignment.rb中遇到了这两段代码你怎么知道第一个是非并行赋值,第二个是一个变量的并行赋值?在我看来,除了命名差异之外,代码几乎完全相同。4deftest_non_parallel_assignment5names=["John","Smith"]6assert_equal["John","Smith"],names7end45deftest_parallel_assignment_with_one_variable46first_name,=["John","Smith"]47assert_equal'John
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]