Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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_Sorting_Matching - Fatal编程技术网

ruby中的第二字段排序

ruby中的第二字段排序,ruby,sorting,matching,Ruby,Sorting,Matching,我非常喜欢Ruby,但我不能用Perl做某些我能做的事情,因此请帮助我 如何匹配行中的一个或另一个条目 在perl中: while ( <$HAN> ) { next unless /**$TAB_11|$TAB_35**/; 我试过了,或者,| |,并且-没有一个像Perl那样工作-我怎么能做到这一点 我试图对数组进行排序,但排序不正确-我需要按第二个字段->日期/时间对其排序 unless line.include? (tag_466 && tag_1004

我非常喜欢Ruby,但我不能用Perl做某些我能做的事情,因此请帮助我

如何匹配行中的一个或另一个条目

在perl中:

while ( <$HAN> ) {
  next unless /**$TAB_11|$TAB_35**/;
我试过了,或者,| |,并且-没有一个像Perl那样工作-我怎么能做到这一点

我试图对数组进行排序,但排序不正确-我需要按第二个字段->日期/时间对其排序

unless line.include? (tag_466 && tag_1004)   
a=[]
a.push "******************************** 1.Message Received On S Side ********************************",
 line,"\n" if line =~ /#{MAT_1}/   #li.console_blink, --to check source 
a.push "******************************** 3.Message received ES side ********************************",
 line,"\n" if line =~ /#{MAT_3}/
a.push "******************************** 4.Ticket has been created ********************************",
 line,"\n" if line =~ /#{MAT_4}/
a.sort!
puts a end`
结果

******************************* 2.Message Processed On SGW side ********************************
20120210 08:03:55,872 DEBUG IceTradeMessageMapper -  --------- Processing  Trade [1311883] --------- 

******************************** 1.Message Received On SGW Side ********************************
20120210 08:04:05,404 DEBUG MQReceiver - Receive message "<FIXML><TrdCaptRpt RptID="763"

Re 1。您可以使用类似Perl的正则表达式match=~

如果你想做它包括?是的

next unless line.include?(tag_11) || line.include?(tag_35)
Re 2。我真的不明白你在这里要问什么,但你可能想看看。

1你可以通过一个文件作为一个方便的迭代器,用来测试正则表达式,就像在perl中一样

IO.foreach(li) do |line|
  next unless line =~ /#{tag_11}|#{tag_35}/
  ...
end
2与三个参数一起使用:长的星号填充字符串、词法变量行和文字\n。看来你打算把这三张唱片当作一张唱片。push只会将它们附加到数组中,也许您真的希望它们成为子数组:

a.push [ '**** something ****', line, "\n" ]
然后,您可以使用

a.sort_by! {|e| e[1] }
如果您确实想要一个平面阵列,可以按原样推送并使用将其分组为三个:

a.each_slice(3).sort_by{|e| e[1]}.flatten(1)
稍后:以下是我认为您要求的排序:

irb(main):001:0> a=[]
irb(main):002:0> a.push "** 1.mumble","20120210 08:04:05,404 DEBUG","\n"
irb(main):003:0> a.push "** 3.grumble","20120210 08:04:00,404 DEBUG","\n"
irb(main):004:0> a.push "** 2.mutter","20120210 08:03:05,404 DEBUG","\n"
=> ["** 1.mumble", "20120210 08:04:05,404 DEBUG", "\n", "** 3.grumble", "20120210 08:04:00,404 DEBUG", "\n", "** 2.mutter", "20120210 08:03:05,404 DEBUG", "\n"]

# sort by time stamp
irb(main):005:0> puts a.each_slice(3).sort_by { |label,ts,_| ts }.flatten(1)
** 2.mutter
20120210 08:03:05,404 DEBUG

** 3.grumble
20120210 08:04:00,404 DEBUG

** 1.mumble
20120210 08:04:05,404 DEBUG

# sort by integer value of first number in the label
irb(main):006:0> puts a.each_slice(3).sort_by{ |label,ts,_| label.match(/(\d+)/)[0].to_i }.flatten(1)
** 1.mumble
20120210 08:04:05,404 DEBUG

** 2.mutter
20120210 08:03:05,404 DEBUG

** 3.grumble
20120210 08:04:00,404 DEBUG

Perl和Ruby代码示例分别缺少a}和end。您应该解决这个问题。您需要使用两个由or连接的include?s。逻辑上把两个字符串放在一起?值不是Ruby表达式的工作方式。最好将这两个问题放在不同的帖子中,而不是放在同一篇帖子中。这是因为堆栈溢出的工作原理与其他论坛略有不同。如果您访问帐户时遇到问题,请发送电子邮件team@stackoverflow.com,请不要将后续评论作为其他帐户的答案发布,这会破坏问答过程。很好,除非行。包括?tag_466或行。包括?tag_1004已工作,下一行除外~/tag_466 | tag_1004/-这不起作用……但感谢Perl提供的建议,因为您可以看到第二个字段,如果它是第二个字段,那么它是08:03:55=>我需要按该字段排序,或者按1.Message或2.Message我添加到行中的tag_466等变量是什么?如果是,它是下一个,除非行=~/{tag_466}{tag_1004}/所以它们的内容被插入正则表达式中。@TomasKlein既然你是新来的,我想指出,在这里向上投票和/或接受答案是一种很好的礼仪没有解决-它复制了结果,没有对它们进行排序A.每个切片3.按{124; e | e[1]}排序U.扁平化1-也没有看到下面的结果似乎是一件复杂的事情,但它必须是这样做的*********************************************************************************6.C正在进行中**********************************************************************************************************************************8:04:999[23]890******************************************************************************************************20120210 08:04:05044调试ICELocallocationin]插入的消息。段数:[1]******************************************************************************************************************20120210 08:03:55872调试IceTradeMessag感谢IO::foreach->Useful模块-我稍后会尝试我没有投票给Michael的原因是速度很重要,我认为我们需要高效地编写,否则我们的脚本语言无法完成,谢谢你的建议,尽管他们仍在尝试排序……有人吗?有什么想法如何排序吗?@TomasKlein你的问题仍然有点混乱。准确显示您的输入和预期输出,我们可以告诉您如何排序。
a.each_slice(3).sort_by{|e| e[1]}.flatten(1)
irb(main):001:0> a=[]
irb(main):002:0> a.push "** 1.mumble","20120210 08:04:05,404 DEBUG","\n"
irb(main):003:0> a.push "** 3.grumble","20120210 08:04:00,404 DEBUG","\n"
irb(main):004:0> a.push "** 2.mutter","20120210 08:03:05,404 DEBUG","\n"
=> ["** 1.mumble", "20120210 08:04:05,404 DEBUG", "\n", "** 3.grumble", "20120210 08:04:00,404 DEBUG", "\n", "** 2.mutter", "20120210 08:03:05,404 DEBUG", "\n"]

# sort by time stamp
irb(main):005:0> puts a.each_slice(3).sort_by { |label,ts,_| ts }.flatten(1)
** 2.mutter
20120210 08:03:05,404 DEBUG

** 3.grumble
20120210 08:04:00,404 DEBUG

** 1.mumble
20120210 08:04:05,404 DEBUG

# sort by integer value of first number in the label
irb(main):006:0> puts a.each_slice(3).sort_by{ |label,ts,_| label.match(/(\d+)/)[0].to_i }.flatten(1)
** 1.mumble
20120210 08:04:05,404 DEBUG

** 2.mutter
20120210 08:03:05,404 DEBUG

** 3.grumble
20120210 08:04:00,404 DEBUG