[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby's equivalent of PHP explode

voipfc

9/26/2006 1:42:00 PM


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

15 Answers

Farrel Lifson

9/26/2006 1:50:00 PM

0

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
>
>
>

irb(main):006:0> "this is a string".split
=> ["this", "is", "a", "string"]

Farrel

Jacob Fugal

9/26/2006 2:33:00 PM

0

On 9/26/06, Farrel Lifson <farrel.lifson@gmail.com> wrote:
> 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.
>
> 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:

irb>> "this is a string".split('s')
=> ["thi", " i", " a ", "tring"]

irb>> "this is a string".split(/[aeiou]/)
=> ["th", "s ", "s ", " str", "ng"]

Jacob Fugal

Paul Lutus

9/26/2006 4:11:00 PM

0

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.ara...

Ryan Eibling

9/26/2006 4:42:00 PM

0

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']

James Gray

9/26/2006 4:44:00 PM

0

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

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


Paul Lutus

9/26/2006 5:53:00 PM

0

James Edward Gray II wrote:

> 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
>
> No 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.

--
Paul Lutus
http://www.ara...

voipfc

9/26/2006 8:31:00 PM

0


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.ara...

Mike Dvorkin

9/26/2006 8:56:00 PM

0

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.rubyw...


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
>> 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.ara...
>
>


voipfc

10/1/2006 8:50:00 AM

0


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
> > 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']

darren kirby

10/1/2006 2:04:00 PM

0

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://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972