String.split crashes my program

T

Tomas Na

I am trying to split a string by spaces and then loop through each
entry, adding it to an array if it is more then 4 characters long and
not already in the array.

I haven't got very far with it, but I have come up against this problem
where split just crashes my program with no errors.

def GetUniqueTextValues(sText)
aTags = Array.new

puts sText
aTags = sText.split
puts sText

return aTags
end

What happens when I run this is the text is output for the first puts,
but not for the second. If I comment out the split then the text is
output twice, once for each puts. I read in the ruby docs that split
with no parameters will split by space, but I also tried split(' ') and
split(/ /) with the same crashing effect. I use split another place in
my program like this:

saHeadParts = line.split(/: /)

and it works fine, so I don't understand why it wont work this time. Can
anyone help me fix this?
 
L

Loga Ganesan

Tomas said:
I am trying to split a string by spaces and then loop through each
entry, adding it to an array if it is more then 4 characters long and
not already in the array.

I haven't got very far with it, but I have come up against this problem
where split just crashes my program with no errors.

def GetUniqueTextValues(sText)
aTags = Array.new

puts sText
aTags = sText.split
puts sText

return aTags
end

What happens when I run this is the text is output for the first puts,
but not for the second. If I comment out the split then the text is
output twice, once for each puts. I read in the ruby docs that split
with no parameters will split by space, but I also tried split(' ') and
split(/ /) with the same crashing effect. I use split another place in
my program like this:

saHeadParts = line.split(/: /)

and it works fine, so I don't understand why it wont work this time. Can
anyone help me fix this?


Its working for me. But I couldn't get your actual problem. If you give
me further details as like your code, your ruby version, It would be
useful for me
to track the problem.
 
R

Robert Klemme

I am trying to split a string by spaces and then loop through each
entry, adding it to an array if it is more then 4 characters long and
not already in the array.

Well, you could do that in one go:

tags = Set.new
....

s_text.scan(/\S{5,}/) {|match| tags << match}
I haven't got very far with it, but I have come up against this problem
where split just crashes my program with no errors.

def GetUniqueTextValues(sText)

Convention in Ruby is to use lower case names for methods and also
separate words by underscores. This avoids confusing methods with classes.

def get_unique_text_values
aTags = Array.new

That Array creation is superfluous.
puts sText
aTags = sText.split
puts sText

return aTags
end

What happens when I run this is the text is output for the first puts,
but not for the second.

This is not exactly what you called a "crash" unless the process would
terminate. Does it? If not, how long did you wait for the split to
complete? What version of Ruby are you using and what is your input
String - especially how long is it?
If I comment out the split then the text is
output twice, once for each puts. I read in the ruby docs that split
with no parameters will split by space, but I also tried split(' ') and
split(/ /) with the same crashing effect.> I use split another place in
my program like this:

saHeadParts = line.split(/: /)

and it works fine, so I don't understand why it wont work this time. Can
anyone help me fix this?

Please provide more information.

Kind regards

robert
 
M

Mark S Bilk

I am trying to split a string by spaces and then loop through each
entry, adding it to an array if it is more then 4 characters long and
not already in the array.

I haven't got very far with it, but I have come up against this problem
where split just crashes my program with no errors.

def GetUniqueTextValues(sText)
  aTags = Array.new

  puts sText
  aTags = sText.split
  puts sText

  return aTags
end

What happens when I run this is the text is output for the first puts,
but not for the second. If I comment out the split then the text is
output twice, once for each puts. I read in the ruby docs that split
with no parameters will split by space, but I also tried split(' ') and
split(/ /) with the same crashing effect. I use split another place in
my program like this:

saHeadParts = line.split(/: /)

and it works fine, so I don't understand why it wont work this time. Can
anyone help me fix this?

I'm very new at Ruby, but that code works for me:
--------------------------------------------
#!/usr/bin/ruby

def GetUniqueTextValues(sText)
aTags = Array.new

puts sText
aTags = sText.split
puts sText

return aTags
end

a = GetUniqueTextValues('mary had a little lamb')
a.each { |x| puts x }
--------------------------------------------
prints:
mary had a little lamb
mary had a little lamb
mary
had
a
little
lamb
--------------------------------------------
I'm using:
ruby 1.8.6 (2007-06-07 patchlevel 36) [i586-linux]
in OpenSuSE Linux 10.3, kernel 2.6.22.19-0.2

It works the same without the Array.new line.
 
T

Tomas Na

It reaches a block of code which causes it to become unresponsive. That
to me indicates a crash. Maybe not to you, but this discussion isn't
about semantics.

Here is my output:
C:\Users\Tomas\Ruby>ruby test.rb
Split this text

Then nothing. We sit here for ever while it "hasnt" crashed.

My Ruby version is:
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
Running on Windows 7


The other split that works is:
def ProcessHeaders(sHead)
hHeaders = {}
sHead.each{|line|
saHeadParts = line.split(/: /)
hHeaders[saHeadParts[0]]=saHeadParts[1]
}
return hHeaders
end

For processing HTTP headers.

I appreciate that my Ruby coding conventions may not match the standard
but I have been programming Ruby for only a day so far and I came here
from C++, so I was just sticking to what I know. That can be refactored
later, I'd rather have a working program than a conventional one.

I tried your scan example which did what I was trying to do, so if all
else fails I will use that. Thanks for the help.
 
T

Todd Benson

It reaches a block of code which causes it to become unresponsive. That
to me indicates a crash. Maybe not to you, but this discussion isn't
about semantics.

Here is my output:
C:\Users\Tomas\Ruby>ruby test.rb
Split this text

Then nothing. We sit here for ever while it "hasnt" crashed.

The fact that you are not even getting to that first puts suggests you
are never calling the method.

Todd
 
T

Tomas Na

Todd said:
The fact that you are not even getting to that first puts suggests you
are never calling the method.

Todd


My text was "Split this text" - That is the first puts being executed.
 
M

Mark S Bilk

It reaches a block of code which causes it to become unresponsive. That
to me indicates a crash. Maybe not to you, but this discussion isn't
about semantics.

Here is my output:
C:\Users\Tomas\Ruby>ruby test.rb
Split this text

Then nothing. We sit here for ever while it "hasnt" crashed.

So "Split this text" is the string you're scanning?
Seems like the program is in a loop, rather than crashed.
Would you please copy and paste the entire exact content
of test.rb into a Usenet article in between two lines
of dashes ("-------------")?
Even better would be to zip it and put it on a website.
Maybe you have some weird characters in the file.
 
R

Robert Klemme

So "Split this text" is the string you're scanning?
Seems like the program is in a loop, rather than crashed.
Would you please copy and paste the entire exact content
of test.rb into a Usenet article in between two lines
of dashes ("-------------")?
Even better would be to zip it and put it on a website.
Maybe you have some weird characters in the file.

pastie would work as well. I agree, we should see the whole program
that is misbehaving, or better the smallest program that exhibits the
described behavior.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top