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?
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
···
On Thu, 4 Aug 2005, Joe Van Dyk wrote:
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?
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?
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?
which will have exactly the right semantics in your case.
···
On 8/3/05, David A. Black <dblack@wobblini.net> wrote:
Hi --
On Thu, 4 Aug 2005, Joe Van Dyk wrote:
> 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:
which will have exactly the right semantics in your case.
Ah... I was trying to do instance_eval. Why didn't that work?
···
On 8/3/05, Caleb Clausen <vikkous@gmail.com> wrote:
On 8/3/05, David A. Black <dblack@wobblini.net> wrote:
> Hi --
>
> On Thu, 4 Aug 2005, Joe Van Dyk wrote:
>
> > 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
> dblack@wobblini.net
>
>
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
···
On Thu, 4 Aug 2005, Caleb Clausen wrote:
On 8/3/05, David A. Black <dblack@wobblini.net> wrote:
Hi --
On Thu, 4 Aug 2005, Joe Van Dyk wrote:
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?