Mefistofeles
Mefistofeles

Reputation: 89

How to convert a comma-delimited string into an array

I'm trying to make a array out of a string and then get rid of the quotes surrounding the strings.

This is what I tried:

hg = "'Erra', 'Erra-Network', 'Discovery'".split(",")
hg2 = hg.each { |n| n.delete_prefix("'").delete_suffix("'") }
print(hg2)

but doesn't work.

Output:

["'Erra'", " 'Erra-Network'", " 'Discovery'"]

Upvotes: 2

Views: 120

Answers (4)

the Tin Man
the Tin Man

Reputation: 160621

You're making it too difficult. I'd do this:

hg = "'Erra', 'Erra-Network', 'Discovery'"

hg.delete(" '").split(',') # => ["Erra", "Erra-Network", "Discovery"]

delete is doing the clean-up, which should be done before trying to split the string.

delete_prefix and delete_suffix are useful methods, but not when processing strings you're parsing because they force you into iteratively processing the sub-strings. delete does the entire string in one step, which is faster.

If I was going to iterate, I'd do something like this:

hg.split(/, */).map { |s| s[1..-2] } # => ["Erra", "Erra-Network", "Discovery"]

which takes advantage of split being able to take a regular expression to automatically break on a comma followed by any number of spaces. Basically you'll trying to parse a mangled CSV record, so you should use the CSV class. The documentation has many examples for parsing records:

require 'csv'
CSV.parse(hg.delete("' ")).flatten # => ["Erra", "Erra-Network", "Discovery"]

CSV has many options available for handling odd variations of delimiters and quoted strings, so study the documentation if you want to go that way.

Upvotes: 0

3limin4t0r
3limin4t0r

Reputation: 21160

You could use CSV to get your result:

require 'csv'

string = "'Erra', 'Erra-Network', 'Discovery'"
result = CSV.parse_line(string, col_sep: ', ', quote_char: "'")
#=> ["Erra", "Erra-Network", "Discovery"]

Note that the above assumes that the separator is ', '. This means that a comma must always be followed by a space.

Upvotes: 0

Sebastián Palma
Sebastián Palma

Reputation: 33481

You're working on hg but then printing hg2 to check if the operation succeeded. Although that's not the problem, it may lead to confusion.

Also, as you're using delete_prefix and delete_suffix in their non-destructive versions, the changes applied return a new object, which isn't "persisted" anywhere.

If you want to see the changes that that produces you can use their destructive version delete_prefix!, delete_suffix!:

hg2 = hg.each do |n|
  n.delete_prefix!("'")
  n.delete_suffix!("'")
end

hg # ["Erra", " 'Erra-Network", " 'Discovery"]

Or rather use map and yield the result of every operation into a new object:

hg2 = hg.map { |n| n.delete_prefix("'").delete_suffix("'") }

p hg2 # ["Erra", " 'Erra-Network", " 'Discovery"]

Although this results in ["Erra", " 'Erra-Network", " 'Discovery"] and doesn't go according to the title of the question "delete the quotes surrounding the strings".

Upvotes: 1

Martin Owen
Martin Owen

Reputation: 5271

Try map instead of each, it will return a new updated Array:

hg = "'Erra', 'Erra-Network', 'Discovery'".split(",")
hg2 = hg.map { |n| n.delete_prefix("'").delete_suffix("'") }
print(hg2)

each will execute for each element in the Array, but will then return the original unmodified Array. map actually returns the modified Array: https://stackoverflow.com/a/5254192/44733

Upvotes: 4

Related Questions