On Thursday, March 11, 2004, 8:19:41 AM, Horacio wrote:

> Hi everyone:
>     I'm a novice ruby user, but ancient user in Linux world. We assume
> have a data file with two data field in two columns. In awk program when I
> like time them a script code may be:

> {
>     print $1*$2
> }

> for example I can use "% cat foo.dat|awk -f times.awk" d'accorde. Well,
> how can I do this in ruby code?

  # times.rb

  ARGF.each do |line|
    puts line if $DEBUG                    # -> "2.0   5.5" for instance
    nums = line.split.map { |n| n.to_f }   # -> [2.0, 5.5]
    puts nums[0] * nums[1]                 # -> 11
    a, b = nums
    puts a * b                             # -> 11
    puts nums.inject { |a,x| a*x }         # -> 11, but allows several columns
  end

Note that contains several alternatives to give you some ideas.  This
is how I would write times.rb:

  # times.rb, v2

  puts ARGF.map { |line|
    line.split.inject(1) { |a,x| a * x.to_f }
  }

This version converts the entire input into an array of results
(that's what 'map' does) and prints them one per line (that's what
'puts' does when given an array).

Hope this helps!

Gavin

PS. All code untested.