regex question

J

Joe Van Dyk

I have a file that contains the following contents:

sw_corner =3D 1000,-1000
ne_corner =3D -1000,1000

I want to read that file and figure out what the sw_corner and
ne_corner values are. Here's my following attempt, but it looks ugly.
How can I improve it?

sw_corner =3D map_data.scan(/sw_corner =3D ([-\d]+),\s*([-\d]+)/)[0]
ne_corner =3D map_data.scan(/ne_corner =3D ([-\d]+),\s*([-\d]+)/)[0]

Thanks,
Joe
 
A

Ara.T.Howard

I have a file that contains the following contents:

sw_corner = 1000,-1000
ne_corner = -1000,1000

I want to read that file and figure out what the sw_corner and
ne_corner values are. Here's my following attempt, but it looks ugly.
How can I improve it?

sw_corner = map_data.scan(/sw_corner = ([-\d]+),\s*([-\d]+)/)[0]
ne_corner = map_data.scan(/ne_corner = ([-\d]+),\s*([-\d]+)/)[0]

Thanks,
Joe

(untested)

h = {}
map_data.each do |line|
key, value = line.split %r/=/
h[k.strip] = value.split(%r/,/).map{|n| Integer n}
end
p h['sw_corner']
p h['nw_corner']

hth.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| My religion is very simple. My religion is kindness.
| --Tenzin Gyatso
===============================================================================
 
D

David A. Black

Hi --

I have a file that contains the following contents:

sw_corner = 1000,-1000
ne_corner = -1000,1000

I want to read that file and figure out what the sw_corner and
ne_corner values are. Here's my following attempt, but it looks ugly.
How can I improve it?

sw_corner = map_data.scan(/sw_corner = ([-\d]+),\s*([-\d]+)/)[0]
ne_corner = map_data.scan(/ne_corner = ([-\d]+),\s*([-\d]+)/)[0]

Here's one possibility, if you want them as integers:

require 'scanf'

map_data = <<EOM
sw_corner = 1000,-1000
ne_corner = -1000,1000
EOM

format = "%*s = %d,%d"

sw = map_data.scanf(format)
ne = map_data.scanf(format)

puts "sw is #{sw.inspect},ne is #{ne.inspect}"


David
 
C

Caleb Clausen

Then there's always:

eval "filename" #;)

which will have exactly the right semantics in your case.=20

Hi --
=20
On Thu, 4 Aug 2005, Joe Van Dyk wrote:
=20
I have a file that contains the following contents:

sw_corner =3D 1000,-1000
ne_corner =3D -1000,1000

I want to read that file and figure out what the sw_corner and
ne_corner values are. Here's my following attempt, but it looks ugly.
How can I improve it?

sw_corner =3D map_data.scan(/sw_corner =3D ([-\d]+),\s*([-\d]+)/)[0]
ne_corner =3D map_data.scan(/ne_corner =3D ([-\d]+),\s*([-\d]+)/)[0]
=20
Here's one possibility, if you want them as integers:
=20
require 'scanf'
=20
map_data =3D <<EOM
sw_corner =3D 1000,-1000
ne_corner =3D -1000,1000
EOM
=20
format =3D "%*s =3D %d,%d"
=20
sw =3D map_data.scanf(format)
ne =3D map_data.scanf(format)
=20
puts "sw is #{sw.inspect},ne is #{ne.inspect}"
=20
=20
David
=20
--=20
=20
=20
David A. Black
(e-mail address removed)
=20
 
J

Joe Van Dyk

Then there's always:
=20
eval "filename" #;)
=20
which will have exactly the right semantics in your case.

Ah... I was trying to do instance_eval. Why didn't that work?
 
W

William James

Joe said:
I have a file that contains the following contents:

sw_corner = 1000,-1000
ne_corner = -1000,1000

I want to read that file and figure out what the sw_corner and
ne_corner values are. Here's my following attempt, but it looks ugly.
How can I improve it?

sw_corner = map_data.scan(/sw_corner = ([-\d]+),\s*([-\d]+)/)[0]
ne_corner = map_data.scan(/ne_corner = ([-\d]+),\s*([-\d]+)/)[0]

map_data = DATA.read

x = map_data.scan( /(sw|ne)_corner = (-?\d+),\s*(-?\d+)/ )
sw_corner = x.assoc("sw")[1,2]
ne_corner = x.assoc("ne")[1,2]

__END__
sw_corner = 1000,-1000
ne_corner = -1000,1000
 
D

David A. Black

Hi --

Then there's always:

eval "filename" #;)

which will have exactly the right semantics in your case.

But variable scope problems:

$ cat corners.txt
sw_corner = 1000,-1000
ne_corner = -1000,1000
$ ruby -e 'eval(File.read("corners.txt")); p sw_corner'
-e:1: undefined local variable or method `sw_corner' for main:Object
(NameError)

(Yes, you can pre-initialize them. It still seems fragile and has the
usual eval issues.)


David
Hi --

I have a file that contains the following contents:

sw_corner = 1000,-1000
ne_corner = -1000,1000

I want to read that file and figure out what the sw_corner and
ne_corner values are. Here's my following attempt, but it looks ugly.
How can I improve it?

sw_corner = map_data.scan(/sw_corner = ([-\d]+),\s*([-\d]+)/)[0]
ne_corner = map_data.scan(/ne_corner = ([-\d]+),\s*([-\d]+)/)[0]

Here's one possibility, if you want them as integers:

require 'scanf'

map_data = <<EOM
sw_corner = 1000,-1000
ne_corner = -1000,1000
EOM

format = "%*s = %d,%d"

sw = map_data.scanf(format)
ne = map_data.scanf(format)

puts "sw is #{sw.inspect},ne is #{ne.inspect}"


David

--


David A. Black
(e-mail address removed)
 

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

Staff online

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,501
Latest member
log5Sshell/alfa5

Latest Threads

Top