Principle of most suprise

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

is it too much to assume that the string is split using [xxxx] as a demliter
and that tda.first should return “+ 30 Jan 12:20:09”?

i’m getting: + 30 Jan 12:20:09 addr: x.x.x.x

i seem to be getting hung up on the simplest things. i’m using
’ruby in a nutshell’ and ‘programming ruby’ as my references.

···


http://home.cogeco.ca/~tsummerfelt1

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

tda.first.first should work here.

split() returns an array, and you pack it into another array.
To fix your code, do:

tda = logline.split(/[\d+]/)
tda.first

···

is it too much to assume that the string is split using [xxxx] as a demliter
and that tda.first should return “+ 30 Jan 12:20:09”?

i’m getting: + 30 Jan 12:20:09 addr: x.x.x.x

i seem to be getting hung up on the simplest things. i’m using
‘ruby in a nutshell’ and ‘programming ruby’ as my references.

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=logline.split(/[\d+]/)
tda.first

is it too much to assume that the string is split using [xxxx] as a demliter
and that tda.first should return “+ 30 Jan 12:20:09”?

Now tda.first returns "+ 30 Jan 12:20:09 ". That help?

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

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

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 ‘’ :-))

Cheers,

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

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

Dave

tony summerfelt snowzone5@hotmail.com writes:

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

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

tony summerfelt snowzone5@hotmail.com wrote in message news:E4zSb.719$_62.294@read1.cgocable.net
[…]

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.

Dave Lee wrote:

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

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+])/]

split will always return an Array (see "Ruby in a nutshell, p. 54).

i read that a number of times before i posted…that’s why i was stumped

You do not have to force this explicitly by using the Array constructor.

i don’t know what i was thinking…you’d never know it, but i’m a better
programmer than that :confused:

And if you want to do so, you have to use ‘()’ not ‘’ :-))

i want to thank everyone for the polite replies :slight_smile: it must have been hard
restraining yourselves :slight_smile:

···

On Fri, 30 Jan 2004 at 21:24 GMT, Maik Schmidt contact@maik-schmidt.de wrote:


http://home.cogeco.ca/~tsummerfelt1

“tony summerfelt” snowzone5@hotmail.com schrieb im Newsbeitrag
news:NbeTb.1048$_62.314@read1.cgocable.net

split will always return an Array (see "Ruby in a nutshell, p. 54).

i read that a number of times before i posted…that’s why i was stumped

You do not have to force this explicitly by using the Array
constructor.

i don’t know what i was thinking…you’d never know it, but i’m a better
programmer than that :confused:

And if you want to do so, you have to use ‘()’ not ‘’ :-))

i want to thank everyone for the polite replies :slight_smile: it must have been
hard
restraining yourselves :slight_smile:

Just to keep improving: change

logline=String.new(“+ 30 Jan 12:20:09 [3988] addr: x.x.x.x”)

to

logline=“+ 30 Jan 12:20:09 [3988] addr: x.x.x.x”

which saves you a superfluous object creation. :slight_smile:

To make results look a bit nicer you could also do

result = logline.split(/\s*[\d+]\s*/).first

to make the regexp eat up all white space that surrounds your split mark.

Regards

robert
···

On Fri, 30 Jan 2004 at 21:24 GMT, Maik Schmidt contact@maik-schmidt.de wrote:

Joel VanderWerf vjoel@PATH.Berkeley.EDU wrote in message news:401ADCE4.2000209@path.berkeley.edu

Dave Lee wrote:

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

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.

Just to keep improving: change

logline=String.new(“+ 30 Jan 12:20:09 [3988] addr: x.x.x.x”)
to
logline=“+ 30 Jan 12:20:09 [3988] addr: x.x.x.x”

yup, thought of that one already :slight_smile:

To make results look a bit nicer you could also do

i just needed to get the string in usable format, for testing the age.

···

On Mon, 02 Feb 2004 at 09:13 GMT, Robert Klemme bob.news@gmx.net wrote:


http://home.cogeco.ca/~tsummerfelt1