I believe the problem here is that split RETURNs an array. You’re creating a
new array out of what is already an array, and therefore getting a new
array containing one element, which is an array.
Just do
tda = logline.split(/.../)
-Mark
···
On Fri, Jan 30, 2004 at 08:25:08PM +0000, tony summerfelt wrote:
gah, ruby is doing it to me again:
logline=String.new(“+ 30 Jan 12:20:09 [3988] addr: x.x.x.x”)
tda=Array[(logline.split(/[\d+]/))]
tda.first
logline=String.new(“+ 30 Jan 12:20:09 [3988] addr: x.x.x.x”)
tda=Array[(logline.split(/[\d+]/))]
tda.first
Try this
tda = logline.split(/[\d+]/)
split will always return an Array (see "Ruby in a nutshell, p. 54).
You do not have to force this explicitly by using the Array constructor.
And if you want to do so, you have to use ‘()’ not ‘’ :-))
logline=String.new(“+ 30 Jan 12:20:09 [3988] addr: x.x.x.x”)
tda=Array[(logline.split(/[\d+]/))]
tda.first
split already returns an Array - no need to create one:
tda= logline.split(/[\d+]/)
tda.first
=> "+ 30 Jan 12:20:09 "
(what you did was ‘obtain the array generated by split and stuff it as the
first element into a new array’, thus getting
[["+ 30 Jan 12:20:09 ", “addr: x.x.x.x”]])
HTH & kind regards
frank
···
–
Frank Schmitt
quattro research GmbH
e-mail: schmitt NO at SPAM quattro-research !@! dot com
i seem to be getting hung up on the simplest things. i’m using
‘ruby in a nutshell’ and ‘programming ruby’ as my references.
Remember to use ‘p variable’ to see what your object actually
contains. This makes it much easy to solve this type of problem.
One of the hardest thing with Ruby is realising all the stuff you
don’t have to do. In a language like Java or VB you are always having
to keep the type at the front of your mind and proceed with caution.
With Ruby you have to learn some laziness.
logline=String.new(“+ 30 Jan 12:20:09 [3988] addr: x.x.x.x”)
tda=Array[(logline.split(/[\d+]/))]
tda.first
another way of writing this code, which you may find more explicit if you
only use tda.first is to use MatchData#pre_match
logline = “+ 30 Jan 12:20:09 [3988] addr: x.x.x.x”
marker = /[\d+]/
marker.match(logline).pre_match # equivalent to tda.first from above
Or using the lookahead contruct, ?=, you can do this:
logline = “+ 30 Jan 12:20:09 [3988] addr: x.x.x.x”
logline[/^.*(?=[\d+])/]
Another alternative is:
logline=String.new(“+ 30 Jan 12:20:09 [3988] addr: x.x.x.x”)
tda, number,address=(logline.split(/[|]|(?:addr:)/, 3))
puts “Putting it all back together: #{tda} [#{number}] addr: #{address}”
The most surprising thing is that programming can be so easy.