Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to assign the contents of a variable to an array

Status
Not open for further replies.

peterv12

Technical User
Dec 31, 2008
108
US
I have a variable that contains the following:

var1 = "01","02","03","04"

I want to put the contents of that variable into an array. I'm new to Ruby, so this is something I haven't tried before, and I don't know if it can be done. I tried:

a1 = Array.[](#{var1}) thinking that that would put the value into the parenthesis. This puts the entire variable value into the "0" element. I'm looking to put each of the numbers into their own element, which would leave me with an array with 4 elements, "01" through "04". Can anyone tell me what I'm doing incorrectly? Better yet, can anyone tell me how to do this correctly?
Thanks...
 
Something like this
Code:
var1 = [COLOR=#6a5acd]'[/color][COLOR=#ff00ff]"01","02","03","04"[/color][COLOR=#6a5acd]'[/color]
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]var1 = [/color][COLOR=#6a5acd]#{[/color]var1[COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]

[COLOR=#0000ff]# split variable into list[/color]
var1lst = var1.chomp.split([COLOR=#6a5acd]"[/color][COLOR=#ff00ff],[/color][COLOR=#6a5acd]"[/color])
puts [COLOR=#6a5acd]"[/color][COLOR=#ff00ff]var1lst = ([/color][COLOR=#6a5acd]#{[/color]var1lst[COLOR=#6a5acd]}[/color][COLOR=#ff00ff])[/color][COLOR=#6a5acd]"[/color]

[COLOR=#0000ff]# process list elements[/color]
var1lst.each{|[COLOR=#008080]el[/color]|
  puts [COLOR=#6a5acd]"[/color][COLOR=#6a5acd]#{[/color]var1lst.index(el)[COLOR=#6a5acd]}[/color][COLOR=#ff00ff]. element = [/color][COLOR=#6a5acd]#{[/color]el[COLOR=#6a5acd]}[/color][COLOR=#6a5acd]"[/color]
}
Output:
Code:
var1 = "01","02","03","04"
var1lst = ("01""02""03""04")
0. element = "01"
1. element = "02"
2. element = "03"
3. element = "04"
 
Hi

peterv12 said:
I want to put the contents of that variable into an array.
Huh ? That is already an array.
Code:
[blue]irb(main):001:0>[/blue] var1 = "01","02","03","04"
=> ["01", "02", "03", "04"]

[blue]irb(main):002:0>[/blue] var1.class
=> Array
peterv12 said:
a1 = Array.[](#{var1})
No idea what are you trying there, but I think you want one of these :
Code:
[blue]irb(main):001:0>[/blue] var1 = "01","02","03","04"
=> ["01", "02", "03", "04"]

[gray]# 1) set another pointer to the same array[/gray]
[blue]irb(main):002:0>[/blue] a1=var1
=> ["01", "02", "03", "04"]

[gray]# 2) copy the content in a new array[/gray]
[blue]irb(main):003:0>[/blue] *b1=var1 # copy   
=> ["01", "02", "03", "04"]

[gray]# the difference is visible when you alter the original[/gray]
[blue]irb(main):004:0>[/blue] var1[2]='FOO'
=> "FOO"

[gray]# 1) the pointer accesses the same content[/gray]
[blue]irb(main):005:0>[/blue] a1[2]
=> "FOO"

[gray]# 2) the copy is a separate object[/gray]
[blue]irb(main):006:0>[/blue] b1[2]
=> "03"

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top