From: Robert Klemme Date: 2009-11-23T17:33:27+09:00 Subject: Re: How to match and count 2009/11/23 Ruby Newbee : > 2009/11/22 Robert Klemme : > >> ruby -nae 'BEGIN {$i=0}; $i+=1 if /something/ =~ $F[3]; END {puts $i}' >> file.txt >> ruby -nae 'BEGIN {$i=0}; /something/ =~ $F[3] and $i+=1; END {puts $i}' >> file.txt >> > > oops this is the same way as Perl's. > > I may adjust one point: > > should be: > $F[3] =~ /something/; > > not: >  /something/ =~ $F[3]; Why? > After that replace "ruby" to "perl" on above commands and that will be > working too. Ah, you want a single program to work both for Perl and Ruby. Thanks for sharing! I usually prefer to have the regular expression as the first argument to =~ because for me that seems more natural (the regexp is doing the matching) and IIRC it is a tad faster (but really only a tad). Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/