Printing variable names

D

darren kirby

Hello all,

I have an array of variables here:

foo = [s0, s1, s2, s3, s4, s5, s6, s7]

I need to loop over the array and print each variable's name and value:

s0: 23
s1: 56
s2: 345345

etc....

I cannot seem to find a way to print the name. Is it possible?

Thanks for consideration,
-d
 
D

darren kirby

quoth the Haze Noc:
Wouldn't it be more logical to use a hash?

Perhaps, but the order of the variables is significant. I was thinking I might
create a hash to map the position to the name and vice versa...

Basically what I need to do here is allow all these variables to be accessed
by name and by position. The name of the vars is significant, and I need the
user to be able to dump the values to see each one, hence the original
question.

Perhaps I will think about this more and see if I cannot formulate my needs a
bit better...

Thanks,
-d
 
D

darren kirby

quoth the darren kirby:
quoth the Haze Noc:

Perhaps, but the order of the variables is significant. I was thinking I
might create a hash to map the position to the name and vice versa...

OK, answered my original question:

@registers = [:zero, :at, :v0, :v1, :a0, :a1, :a2, :a3,
:t0, :t1, :t2, :t3, :t4, :t5, :t6, :t7,
:s0, :s1, :s2, :s3, :s4, :s5, :s6, :s7,
:t8, :t9, :k0, :k1, :gp, :sp, :fp, :ra]

@registers.each do |v|
puts "$#{v}: #{send(v)}"
end

...

-d
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top