Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

String to array to string again

How would I convert a string to an array, and an array to a string again?

Something along the lines of:

@enemy_data = [1, 2, 3, 4]
@enemy_data.to_string # returns "1|2|3|4"
@enemy_data.to_array = # returns [1, 2, 3, 4]

I know .to_s and .to_a exist, but I'm not sure they work for what I want to do. .to_s and .to_a would do:

@enemy_data = [1, 2, 3, 4]
@enemy_data.to_s # returns "1234"
@enemy_data.to_a = # returns ["1234"]
 
remember when I sent you that "Network" script for the other server I made? there is a module called "Ex" there that I made, it has the ability to do it. :thumb:

Edit:
here it is:
Code:
module Ex
  def self.s_to_a(string,type="|")
    string = string.gsub(type) {|s| s="\n" }
    slut = []
    string.each {|b|
      b = b.gsub("\n") {|s| s="" }
      slut.push(b)
    }
    return slut
  end
  def self.a_to_s(array,type="|")
    slut = ""
    for i in array
      slut += "#{i}|"
    end
    return slut
  end
  def self.h_to_s(hash)
    slut = ""
    hash.each{|key, value|
    slut += "#{key},#{value}|"
    }
    return slut
  end
  def self.s_to_h(string)
    slut = Hash.new
    string = s_to_a(string)
    length = string.length-1
    for i in 0..length
      string[i] = s_to_a(string[i],",")
    end
    for i in 0..length
      slut[string[i][0]] = string[i][1]
    end
    return slut
  end
end
back then when I made it I didn't know how to add functions to the string and array classes so I did it like this.
 
Btw there's a shorter way (to do this what you asked):
Code:
array = [1, 2, 3, 4]
p array.join('|')  # returns "1|2|3|4"

Code:
str = '1|2|3|4'
array = Array.new
str.split('|').each{|i| array << i.to_i}
p array # returns [1, 2, 3, 4]

And nice script, S S Muu ^_^
 
well, that works too.

thanks. :smile:

Edit:
I remade it for you so it's easier to use and better coded :thumb:

Code:
class String
  def to_array(type = '|')
    self.split(type)
  end
  def to_hash
    ret = {}
    str = to_array
    str.each{|i| a = i.to_array(',');ret[a[0]] = a[1]}
    ret
  end
end

class Array
  def to_string(type = '|')
    self.join(type)
  end
end

class Hash
  def to_string
    ret = ""
    self.each{|k, v| ret += "#{k},#{v}|"}
    ret
  end
end
and this is how you use it
Code:
"name|32|woot?".to_array
["name", 32, "woot?"].to_string
"name,anton|level,3|other,none".to_hash
{"name" => "anton", "level" => 3, "other" => "none"}.to_string
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top