newbie: argh! array of array

A

Alfonso Caponi

Hi forum,

I'm fighting with a function like this! :)


function(["x","y","x"]) do |a,b|c|
printf("%10s %10s %10s\n",a,b,c)
end


It works fine, but I would like a "pseudo-code" like this :)


new array

function(["x","y","z"]) do |a,b|c|
push a,b,c into array
end

if array is not empty
printf("%10s %10s %10s\n",a,b,c)
end


Which is the best way to write it?

Thank you very very much,
Al
 
B

Bertram Scharpf

Hi,

Am Donnerstag, 24. Dez 2009, 08:32:29 +0900 schrieb Alfonso Caponi:
meth(sth) do |a,b,c|
push a,b,c into array
end
=20
if array is not empty
printf("%10s %10s %10s\n",a,b,c)
end

You maybe mean a situation in that the length of the argument list
can change: |a,b,c,...|. Then you could use splats. Here's an
example:

def fade_away
yield "a", "b", "c"
yield "a", "b"
yield "a"
yield
end

fade_away do |*args|
if args.any? then
puts args.join
else
puts "."
end
end

You can use splats in many other contexts:

def some_method *args
a, b, c, * =3D *args
end

ary =3D [ /^y/, "ja", "oui", "si", "s=ED"]
case answ
when *ary then do_it
end

Everything untested.

Bertram


--=20
Bertram Scharpf
Stuttgart, Deutschland/Germany
*
Discover String#notempty? at <http://raa.ruby-lang.org/project/step>.
 
R

Robert Klemme

Hi forum,

I'm fighting with a function like this! :)


function(["x","y","x"]) do |a,b|c|
printf("%10s %10s %10s\n",a,b,c)
end


It works fine, but I would like a "pseudo-code" like this :)


new array

function(["x","y","z"]) do |a,b|c|
push a,b,c into array
end

if array is not empty
printf("%10s %10s %10s\n",a,b,c)
end


Which is the best way to write it?

I am having difficulties to figure what you want. Do you want to print
arbitrary long arrays of strings with a particular formatting? If so,
you can do any of these

puts arr.map {|x| '%10s' % x}.join(' ')

printf arr.map{'%10s'}.join(' ') << "\n", *arr

Kind regards

robert
 
A

Alfonso Caponi

Alfonso said:
Hi forum,

I'm fighting with a function like this! :)


function(["x","y","x"]) do |a,b|c|
printf("%10s %10s %10s\n",a,b,c)
end


It works fine, but I would like a "pseudo-code" like this :)


new array

function(["x","y","z"]) do |a,b|c|
push a,b,c into array
end

if array is not empty
printf("%10s %10s %10s\n",a,b,c)
end


Which is the best way to write it?

Thank you very very much,
Al

Check it out! :)

array = []

function(["x","y"]) do |a,b|
array.push([["#{a.value}"],["#{b.value}"]])
end

if not array.empty?
array.each {|z,k| printf("%10s %s\n",z,k)}
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top