Novice question, tranlate awk code to ruby code

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?

Thanks Horacio

Of cause this could be simplified a lot… but here is a
simplistic solution.

Is this what you want ?

BTW: welcome to ruby :wink:

···

On Wed, 10 Mar 2004 16:04:21 -0300, Horacio Castellini wrote:

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?


Simon Strandgaard

ruby a.rb
1
2
3
2
4
6
3
6
9
4
8
12
expand -t2 a.rb

create a dummy data file

File.open(“data”, “w+”) do |file|
file.write <<-DATA.gsub(/^\s+/, “”)
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
4 2
4 3
DATA
end

process data

lines = IO.readlines(“data”)
lines.each do |line|
m = /(\d+)\s+(\d+)/.match(line)
unless m
puts “cannot compute”
next
end
lhs = m[1].to_i
rhs = m[2].to_i
p lhs * rhs
end

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.

···

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?

This may not match the terseness of the awk script and
is certainly not the only way in Ruby, but is still a
one-liner (which may get mis-formated by the email
client):

C:\RB>type junk.dat
10 20 30
20 30 40
50 60 70
80 90 40

C:\RB>type junk.dat | ruby -ne “p $_.split(’
').slice(0…1).collect{|e| e.to_i}.inject(1){|p,n|
p*n}”
200
600
3000
7200

C:\RB>ruby -v
ruby 1.8.0 (2003-08-04) [i386-mswin32]

— Horacio Castellini hcaste@fceia.unr.edu.ar
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?

Thanks Horacio


Do you Yahoo!?
Yahoo! Search - Find what you’re looking for faster

Hi. Here’s a very awk-like way to do it:

cat foo.dat |ruby -ane ‘puts $F[0].to_i * $F[1].to_i’

-a autosplits and creates $F, which is an array of the fields on each
line.

-n creates a loop for each line of input.

-e executes the next arg as a ruby program.

One interesting note if you’re coming from awk is that the record
separator (specified by -F on the command line of both Ruby and awk)
can accept a regular expression. So (for a silly example), you could
have a file like this:

this123123124is524234234a987234872test

and executing
ruby -F[0-9]+ -ane ‘puts $F[3]’
…would output “test”

Chad

···

On Mar 10, 2004, at 4:19 PM, Horacio Castellini 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?

Hi!

  • Horacio Castellini:

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?

First some remark on the above command line: It is unnecessarily
complicated,

awk '{print $1 * $2}' times.awk

works as well. Two solutions using Ruby. First one does integer
arithmetics:

ruby -a -n -e 'puts $F[0].to_i * $F[1].to_i'

Second one does floating-point arithmetics:

ruby -a -n -e 'puts $F[0].to_f * $F[1].to_f'

They only differ in the type conversion part.

‘-e’ means that what follows is code to be executed.

‘-n’ makes the Ruby iterate over all lines successively assigning
them to $_

‘-a’ makes Ruby split $_ into its parts assigning the result to $F
which is an array of strings

To specify the column separator you can use the ‘-F’ option that is
familiar from awk.

Please note that awk’s $0 maps to Ruby’s $_ and that $1, $2, … map
to $F[0], $F[1], …

HTH,

Josef ‘Jupp’ SCHUGT

···


E-Mail: .— …- .–. .–. .–.-. --. – -…- .-.-.- -… .
http://oss.erdfunkstelle.de/ruby/ - German comp.lang.ruby FAQ
http://rubyforge.org/users/jupp/ - Ruby projects at Rubyforge
.- .-.

“Chad Fowler” chad@chadfowler.com schrieb im Newsbeitrag
news:66E60584-72F3-11D8-B5D4-000A95BDA42A@chadfowler.com

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?

Hi. Here’s a very awk-like way to do it:

cat foo.dat |ruby -ane ‘puts $F[0].to_i * $F[1].to_i’

This entitles you to a useless cat award. Just do

ruby -ane ‘puts $F[0].to_i * $F[1].to_i’ foo.dat

Or

ruby -ane ‘puts Integer($F[0]) * Integer($F[1])’ foo.dat

robert
···

On Mar 10, 2004, at 4:19 PM, Horacio Castellini wrote:

Chad Fowler 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?

Hi. Here’s a very awk-like way to do it:

cat foo.dat |ruby -ane ‘puts $F[0].to_i * $F[1].to_i’

-a autosplits and creates $F, which is an array of the fields on each line.

-n creates a loop for each line of input.

-e executes the next arg as a ruby program.

Thanks, that is I looked for

···

On Mar 10, 2004, at 4:19 PM, Horacio Castellini wrote: