INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to assign the contents of a variable to an array

How to assign the contents of a variable to an array

How to assign the contents of a variable to an array

(OP)
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...

RE: How to assign the contents of a variable to an array

Something like this

CODE

var1 = '"01","02","03","04"'
puts "var1 = #{var1}"

# split variable into list
var1lst = var1.chomp.split(",")
puts "var1lst = (#{var1lst})"

# process list elements
var1lst.each{|el|
  puts "#{var1lst.index(el)}. element = #{el}"
}
Output:

CODE

var1 = "01","02","03","04"
var1lst = ("01""02""03""04")
0. element = "01"
1. element = "02"
2. element = "03"
3. element = "04"

RE: How to assign the contents of a variable to an array

Hi

Quote (peterv12):

I want to put the contents of that variable into an array.
Huh ? That is already an array.

CODE --> irb

irb(main):001:0> var1 = "01","02","03","04"
=> ["01", "02", "03", "04"]

irb(main):002:0> var1.class
=> Array

Quote (peterv12):

a1 = Array.[](#{var1})
No idea what are you trying there, but I think you want one of these :

CODE --> irb

irb(main):001:0> var1 = "01","02","03","04"
=> ["01", "02", "03", "04"]

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

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

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

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

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

Feherke.
http://free.rootshell.be/~feherke/

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close