Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...This site is a great forum to exchange knowledge..."

Geography

Where in the world do Tek-Tips members come from?

How to assign the contents of a variable to an array

peterv12 (TechnicalUser)
21 Jun 10 21:25
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...
mikrom (Programmer)
22 Jun 10 4:01
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"
feherke (Programmer)
22 Jun 10 4:14
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/

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!

Back To Forum

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