简体   繁体   中英

Using a variable in the name of another variable

How can use a variable in the name of another variable? Say, I have a number of arrays like this:

g_OS = ['Mac', 'Linux', 'Win']
g_Mac = ['Lion', 'Tiger', 'Jaguar']
g_Linux = ['Slackware', 'RedHat', 'Caldera']
g_Win = [ .... ]

If I do this: g_OS.each {|OS| puts "g_#{OS}[0]"} g_OS.each {|OS| puts "g_#{OS}[0]"} , it will print 'g_Mac[0]' or 'g_Linux' as a literal string. But what I actually want is to get the first element of the array: g_Mac . How can I do that?

Generally it's easier just to rearrange your data. Like this, for example.

g_OS = {
  'Mac' => ['Lion', 'Tiger', 'Jaguar'],
  'Linux' => ['Slackware', 'RedHat', 'Caldera'], 
  'Win' => ['Chicago', 'Daytona', 'Longhorn']
}

# list just the OSes

g_OS.keys # => ["Mac", "Linux", "Win"]

# only linuxes
g_OS['Linux'] # => ["Slackware", "RedHat", "Caldera"]

Although it's technically possible to do exactly what you asked, I advise you against it (and will not, therefore, post the code). You seem to be new, so you have lots to learn. Don't learn wrong ways.

This is to answer to my own question, which completely different compare to my original question, but it serves exactly (well, more or less) the same purpose. So, as Sergio suggested: Say the example input file is something like this:

maci:ruby san$ cat OSs.txt 
Slackware, Linux, i-num=1
Jaguar, MacX, i-num=6
Chicago, this_Win, i-num=2
Daytona, an_other_Win, i-num=7
RedHat, Linux, i-num=5
Lion, MacY, i-num=4
Caldera, Linux, i-num=9
Longhorn, that_Win, i-num=8
Tiger, MacZ, i-num=3
Indiana, Solaris, i-num=10
Kodiak, MacX, i-num=11

The actual file is dynamically created with variable number of OSs ie the file may or may not have Mac or Win at all. Taking from there, this is what I came up so far....

inFile = "OSs.txt"
os = {}

open(inFile, 'r').each do |line|
    next if line =~ /^\s*(#|$)/

    if line.split(',').map(&:strip)[1] =~ /^Mac/
        (os[:Mac] ||= []) << line.split(',').map(&:strip)[0]
    end

    if line.split(',').map(&:strip)[1] =~ /_Win$/
        (os[:Win] ||= []) << line.split(',').map(&:strip)[0]
    end

    if line.split(',').map(&:strip)[1] !~ /(^Mac|_Win$)/
        (os[:Linux] ||= []) << line.split(',').map(&:strip)[0]
    end
end

os.each_key do |cls|
    p "%s [%s]" % [os[cls][0], os[cls].count]
end

It's pretty much doing what I actually want but I believe there are a lot better ways of doing it. Cheers!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM