What is ruby's equivalent of PHPs explode?
A routine which can breakdown a string and return the results in an
array or a routine that can parse an .ini file and return the key
value pairs in an array[key]=value.
Frank
What is ruby's equivalent of PHPs explode?
A routine which can breakdown a string and return the results in an
array or a routine that can parse an .ini file and return the key
value pairs in an array[key]=value.
Frank
irb(main):006:0> "this is a string".split
=> ["this", "is", "a", "string"]
Farrel
On 26/09/06, voipfc@googlemail.com <voipfc@googlemail.com> wrote:
What is ruby's equivalent of PHPs explode?
A routine which can breakdown a string and return the results in an
array or a routine that can parse an .ini file and return the key
value pairs in an array[key]=value.Frank
voipfc@googlemail.com wrote:
What is ruby's equivalent of PHPs explode?
A routine which can breakdown a string and return the results in an
array or a routine that can parse an .ini file and return the key
value pairs in an array[key]=value.
Actually, that is two questions. As to the first (explode a string on word
boundaries), you've been given an answer:
array = string.split("\s") # many variations
As to the second, read an .ini file and break it down by line and by
name/value pairs (not tested):
data = File.read("filename.ini")
my_hash = {}
data.each do |line|
key,value = line.split("=")
my_hash[key] = value
end
The second won't work properly if there are any equals signs in the value
field. The solution for this is only a bit more complex.
--
Paul Lutus
http://www.arachnoid.com
>
> What is ruby's equivalent of PHPs explode?
>
> A routine which can breakdown a string and return the results in an
> array or a routine that can parse an .ini file and return the key
> value pairs in an array[key]=value.irb(main):006:0> "this is a string".split
=> ["this", "is", "a", "string"]
Also, can take an argument to split on a character/pattern besides space:
>> "this is a string".split('s')
=> ["thi", " i", " a ", "tring"]
>> "this is a string".split(/[aeiou]/)
=> ["th", "s ", "s ", " str", "ng"]
Jacob Fugal
On 9/26/06, Farrel Lifson <farrel.lifson@gmail.com> wrote:
On 26/09/06, voipfc@googlemail.com <voipfc@googlemail.com> wrote:
No need to slurp a file so we can process it line by line. Also, the equals-in-value problem is just one more argument to split():
ini = Hash.new
File.foreach("file_name.ini") do |line|
key, value = line.split("=", 2)
ini[key] = value
end
James Edward Gray II
On Sep 26, 2006, at 11:15 AM, Paul Lutus wrote:
As to the second, read an .ini file and break it down by line and by
name/value pairs (not tested):data = File.read("filename.ini")
my_hash = {}
data.each do |line|
key,value = line.split("=")
my_hash[key] = value
end
Paul Lutus wrote:
data = File.read("filename.ini")
my_hash = {}
data.each do |line|
key,value = line.split("=")
my_hash[key] = value
end
Here's a modified version that supports sections:
data = File.read("filename.ini")
ini_hash = {}
ini_hash[""] = section_hash = {} # default unnamed section
data.each do |line|
if /^\[.+\]$/ =~ line # section headers are surrounded by brackets
ini_hash[line.chomp.gsub(/[\[\]]/, '')] = section_hash = {}
else
key,value = line.chomp.split("=")
section_hash[key] = value
end
end
# example of walking through the whole thing
ini_hash.each do |k, v|
puts "-#{k}-"
v.each {|sk, sv| puts " #{sk}=#{sv}"}
end
# example of accessing a value by section and key
v = ini_hash['section']['key']
Paul Lutus wrote:
>
> What is ruby's equivalent of PHPs explode?
>
> A routine which can breakdown a string and return the results in an
> array or a routine that can parse an .ini file and return the key
> value pairs in an array[key]=value.Actually, that is two questions. As to the first (explode a string on word
boundaries), you've been given an answer:array = string.split("\s") # many variations
As to the second, read an .ini file and break it down by line and by
name/value pairs (not tested):data = File.read("filename.ini")
my_hash = {}
data.each do |line|
key,value = line.split("=")
my_hash[key] = value
endThe second won't work properly if there are any equals signs in the value
field. The solution for this is only a bit more complex.
Will this fix the multiple = sign problem
data.each do |line|
my_hash[line.slice[0, line.index("=") - 1] =
line.slice[line.index("="), line.length -line.index("=")]
end
I am new to Ruby and rightly or wrongly it looks rather unRuby like,
can it be expressed more elegantly?
James Edward Gray II wrote:
As to the second, read an .ini file and break it down by line and by
name/value pairs (not tested):data = File.read("filename.ini")
my_hash = {}
data.each do |line|
key,value = line.split("=")
my_hash[key] = value
endNo need to slurp a file so we can process it line by line.
Yes, true, this is an old habit of mine dating from the days of floppy
drives as data sources (mechanical wear issues), but obviously with large
files, a potential source of difficulty.
Also, the
equals-in-value problem is just one more argument to split():ini = Hash.new
File.foreach("file_name.ini") do |line|
key, value = line.split("=", 2)
Thanks! Hadn't seen this syntax.
Actually the split method has second parameter where you can specify number of splits you want, i.e.:
data.each do |line|
key, value = line.split(/=/, 2)
end
HTH.
Mike Dvorkin
http://www.rubywizards.com
On Sep 26, 2006, at 1:35 PM, voipfc@googlemail.com wrote:
Paul Lutus wrote:
voipfc@googlemail.com wrote:
What is ruby's equivalent of PHPs explode?
A routine which can breakdown a string and return the results in an
array or a routine that can parse an .ini file and return the key
value pairs in an array[key]=value.Actually, that is two questions. As to the first (explode a string on word
boundaries), you've been given an answer:array = string.split("\s") # many variations
As to the second, read an .ini file and break it down by line and by
name/value pairs (not tested):data = File.read("filename.ini")
my_hash = {}
data.each do |line|
key,value = line.split("=")
my_hash[key] = value
endThe second won't work properly if there are any equals signs in the value
field. The solution for this is only a bit more complex.Will this fix the multiple = sign problem
data.each do |line|
my_hash[line.slice[0, line.index("=") - 1] =
line.slice[line.index("="), line.length -line.index("=")]
endI am new to Ruby and rightly or wrongly it looks rather unRuby like,
can it be expressed more elegantly?--
Paul Lutus
http://www.arachnoid.com
Ryan Eibling wrote:
Paul Lutus wrote:
>
> data = File.read("filename.ini")
>
> my_hash = {}
>
I am trying to understand what {} means in ruby. I understand that it
means an empty block, but what does it mean in this context?
Does it mean nil? Does it simply my_hash as an associative array?
> data.each do |line|
> key,value = line.split("=")
> my_hash[key] = value
> endHere's a modified version that supports sections:
data = File.read("filename.ini")
ini_hash = {}
ini_hash[""] = section_hash = {} # default unnamed sectiondata.each do |line|
if /^\[.+\]$/ =~ line # section headers are surrounded by brackets
ini_hash[line.chomp.gsub(/[\[\]]/, '')] = section_hash = {}
else
key,value = line.chomp.split("=")
section_hash[key] = value
end
end# example of walking through the whole thing
ini_hash.each do |k, v|
puts "-#{k}-"
v.each {|sk, sv| puts " #{sk}=#{sv}"}
end# example of accessing a value by section and key
v = ini_hash['section']['key']
quoth the voipfc@googlemail.com:
Ryan Eibling wrote:
> Paul Lutus wrote:
> > data = File.read("filename.ini")
> >
> > my_hash = {}I am trying to understand what {} means in ruby. I understand that it
means an empty block, but what does it mean in this context?Does it mean nil? Does it simply my_hash as an associative array?
It just creates an empty hash. It is equivalent to:
my_hash = Hash.new
And no, an empty hash is not nil:
irb(main):001:0> {} == nil
=> false
-d
--
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972
I wrote my first ruby program and displayed it here -
http://groups.google.co.uk/group/comp.lang.ruby/browse_frm/thread/d9a5608bdfb5ebbd/dfe7da9069b5bf40?lnk=st&q=voipfc&rnum=1&hl=en#dfe7da9069b5bf40
When I apply some text to it I receive this error message
=======
templatefiller.rb:33:in `fetch': key not found (IndexError)
from templatefiller.rb:33:in `run'
from templatefiller.rb:32:in `run'
from templatefiller.rb:31:in `run'
from templatefiller.rb:126
from c:/webdirectory/ruby/lib/ruby/1.8/find.rb:39:in `find'
from c:/webdirectory/ruby/lib/ruby/1.8/find.rb:38:in `find'
from templatefiller.rb:110
from templatefiller.rb:109
Could anyone explain what is happening?
Mike Dvorkin wrote:
Actually the split method has second parameter where you can specify
number of splits you want, i.e.:data.each do |line|
key, value = line.split(/=/, 2)
endHTH.
Mike Dvorkin
http://www.rubywizards.comOn Sep 26, 2006, at 1:35 PM, voipfc@googlemail.com wrote:
>
> Paul Lutus wrote:
>> voipfc@googlemail.com wrote:
>>
>>>
>>> What is ruby's equivalent of PHPs explode?
>>>
>>> A routine which can breakdown a string and return the results in an
>>> array or a routine that can parse an .ini file and return the key
>>> value pairs in an array[key]=value.
>>
>> Actually, that is two questions. As to the first (explode a string
>> on word
>> boundaries), you've been given an answer:
>>
>> array = string.split("\s") # many variations
>>
>> As to the second, read an .ini file and break it down by line and by
>> name/value pairs (not tested):
>>
>> data = File.read("filename.ini")
>>
>> my_hash = {}
>>
>> data.each do |line|
>> key,value = line.split("=")
>> my_hash[key] = value
>> end
>>
>> The second won't work properly if there are any equals signs in
>> the value
>> field. The solution for this is only a bit more complex.
>>
>
> Will this fix the multiple = sign problem
>
> data.each do |line|
> my_hash[line.slice[0, line.index("=") - 1] =
> line.slice[line.index("="), line.length -line.index("=")]
> end
>
> I am new to Ruby and rightly or wrongly it looks rather unRuby like,
> can it be expressed more elegantly?
>
>
>> --
>> Paul Lutus
>> http://www.arachnoid.com
>
>
"Does it simply my_hash as an associative array?"
Hi
I think its better to call arrays arrays, and hashes hashes in Ruby.
I guess php simplifies this a bit too much for my taste,
with array() uniting both arrays and hashes.
Hash.new is the same as {} in ruby, although the second is a
tiny bit faster as far as I know.
nil means that something doesnt really exist, even an empty
'' string is not nil
--
Posted via http://www.ruby-forum.com/.
Marc Heiler wrote:
"Does it simply my_hash as an associative array?"
Hi
I think its better to call arrays arrays, and hashes hashes in Ruby.
Isn't a ruby hash the same what some other languages call an
associative array?
I guess php simplifies this a bit too much for my taste,
with array() uniting both arrays and hashes.Hash.new is the same as {} in ruby, although the second is a
tiny bit faster as far as I know.nil means that something doesnt really exist, even an empty
'' string is not nil--
Posted via http://www.ruby-forum.com/\.
voipfc@googlemail.com wrote:
Isn't a ruby hash the same what some other languages call an
associative array?
Yes. But each language has it's only idiom. For example, python calls
arrays "lists" and assoc. arrays "dictionaries". Ohter languages use
the terms "map", "index", "table" and so forth for assoc. arrays. In
the ruby idiom we say hash.
Regards,
Jordan
quoth the voipfc@googlemail.com:
Marc Heiler wrote:
> "Does it simply my_hash as an associative array?"
> Hi
> I think its better to call arrays arrays, and hashes hashes in Ruby.Isn't a ruby hash the same what some other languages call an
associative array?
Yeah, it's just semantics. I think Marc's point is that when using Ruby it is
best to refer to them as Hashes to prevent confusion, as Ruby officially
calls them hashes. When using PHP call them associative arrays, and in Python
call them dictionaries, for the same reason. When in Rome and all.
Wikipedia has an interesting page on the subject:
-d
--
darren kirby :: Part of the problem since 1976 :: http://badcomputer.org
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972