Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > extract a range start/end?

Reply
Thread Tools

extract a range start/end?

 
 
Michael Linfield
Guest
Posts: n/a
 
      09-02-2007
if i have a list of dates in an array such as:

4/2/07
4/3/07
4/4/07
4/5/07
4/6/07
4/7/07
4/8/07
ect.

how would i..based on option parse..pull a starting point and end point
of to extract that data range.

IE:

opts = OptionParser.new do |opts|
opts.on("-s", "--startdate", "What start date to use." do |i|
#code to determine to use the starting date
end

# the same would apply to an --enddate

any ideas?

i was thinking i could possibly create a new range depending on what the
user input was by doing

require 'date'
results = []

(Date.new(2007,4,1)..Date.new(2007,4,).each {|r| res << r}
but then how would i match that against the original dates for the data?

feel free to give me any thoughts.

-Thanks
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Wilson Bilkovich
Guest
Posts: n/a
 
      09-02-2007
On 9/2/07, Marcin Grzywaczewski <> wrote:
> 2007/9/2, Michael Linfield <>:
> >
> > if i have a list of dates in an array such as:
> >
> > 4/2/07
> > 4/3/07
> > 4/4/07
> > 4/5/07
> > 4/6/07
> > 4/7/07
> > 4/8/07
> > ect.
> >
> > how would i..based on option parse..pull a starting point and end point
> > of to extract that data range.
> >
> > IE:
> >
> > opts = OptionParser.new do |opts|
> > opts.on("-s", "--startdate", "What start date to use." do |i|
> > #code to determine to use the starting date
> > end
> >
> > # the same would apply to an --enddate
> >
> > any ideas?
> >
> > i was thinking i could possibly create a new range depending on what the
> > user input was by doing
> >
> > require 'date'
> > results = []
> >
> > (Date.new(2007,4,1)..Date.new(2007,4,).each {|r| res << r}
> > but then how would i match that against the original dates for the data?
> >
> > feel free to give me any thoughts.
> >
> > -Thanks
> > --
> > Posted via http://www.ruby-forum.com/.

>
>
> I think that range operates on numbers, and method "to_i" casts class to
> number, so your class should have specific method "to_i".
>


From rdoc:
Ranges can be constructed using objects of any type, as long as
the objects can be compared using their <=> operator and they
support the succ method to return the next object in sequence.

In practice, that usually means 'Anything that has Enumerable as an ancestor'

 
Reply With Quote
 
 
 
 
Florian Aßmann
Guest
Posts: n/a
 
      09-02-2007
Hi Michael,
> opts = OptionParser.new do |opts|
> opts.on("-s", "--startdate", "What start date to use." do |i|
> #code to determine to use the starting date


# see Parsedate#parsedate at
http://www.ruby-doc.org/stdlib/libdo...e.html#M001494

@startdate = Parsedate.parsedate i

> end


> i was thinking i could possibly create a new range depending on what the
> user input was by doing
>
> require 'date'
> results = []

# delete this

>
> (Date.new(2007,4,1)..Date.new(2007,4,).each {|r| res << r}

result = (@startdate..@enddate).to_a

> but then how would i match that against the original dates for the data?

match = original & result

Oyasumi
Florian

 
Reply With Quote
 
Florian Aßmann
Guest
Posts: n/a
 
      09-02-2007
little flaw:

Florian A=C3=9Fmann schrieb:
> Hi Michael,
>> opts =3D OptionParser.new do |opts|
>> opts.on("-s", "--startdate", "What start date to use." do |i|
>> #code to determine to use the starting date

>=20

# see Parsedate#parsedate at
#
http://www.ruby-doc.org/stdlib/libdo.../ParseDate.ht=
ml#M001494
>=20

- @startdate =3D Parsedate.parsedate i
+ @startdate =3D Date.new(*ParseDate.parsedate(i)[0,3])

>=20
>> end




 
Reply With Quote
 
Michael Linfield
Guest
Posts: n/a
 
      09-02-2007
Florian Aßmann wrote:
> little flaw:
>
> Florian Aßmann schrieb:
>> Hi Michael,
>>> opts = OptionParser.new do |opts|
>>> opts.on("-s", "--startdate", "What start date to use." do |i|
>>> #code to determine to use the starting date

>>

> # see Parsedate#parsedate at
> #
> http://www.ruby-doc.org/stdlib/libdo...e.html#M001494
>>

> - @startdate = Parsedate.parsedate i
> + @startdate = Date.new(*ParseDate.parsedate(i)[0,3])


if you dont mind my asking,

@startdate = Date.new(*ParseDate.parsedate(i)[0,3])

can someone explain how that line of code works, i'd rather understand
it so that i actually know what it does when i insert it into my code

Thanks!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      09-02-2007
Wilson Bilkovich wrote:
>>From rdoc:

> Ranges can be constructed using objects of any type, as long as
> the objects can be compared using their <=> operator and they
> support the succ method to return the next object in sequence.
>
> In practice, that usually means 'Anything that has Enumerable as an ancestor'


You meant Comparable, right? There's no connection between Enumerable
and #<=> or #succ.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
Florian Aßmann
Guest
Posts: n/a
 
      09-02-2007
Ok, I though you were familiar with some of the RubyDoc ressources...

Here I go:
ParseDate and Date are part of the Ruby StdLib, therefore in order to use this
you need to:
require 'parsedate'
require 'date'

Next thing I think you expect something like '1/20/2007' as dateformat:
/my_app.rb --startdate 1/20/2007

This 'll be parsed by:
ParseDate.parsedate('1/20/2007') # => [2007,1,20,nil,nil,nil]

To generate an instance of Date out of this we simply call Date.new, but wait,
Date.new only accepts 3 args (maybe 4, though).

Therefore I invoke the slice-method ([]) on the freshly baken array:
# I assume i = '1/20/2007'
ParseDate.parsedate(i)[0, 3] # => [2007,1,20]

To split the array into seperate arguments when I invoke Date.new I need the
*-Operator: a_method(*[1,2,3]) is like a_method(1,2,3)

Date.new(*ParseDate.parsedate(i)[0, 3]) # => <Date... >

Tada, you got your Date now...

Further reading:
Ruby Stdlib: Date and ParseDate
Ruby Classes and Libraries: Array.slice

Regards
Florian

 
Reply With Quote
 
Wilson Bilkovich
Guest
Posts: n/a
 
      09-02-2007
On 9/2/07, Joel VanderWerf <> wrote:
> Wilson Bilkovich wrote:
> >>From rdoc:

> > Ranges can be constructed using objects of any type, as long as
> > the objects can be compared using their <=> operator and they
> > support the succ method to return the next object in sequence.
> >
> > In practice, that usually means 'Anything that has Enumerable as an ancestor'

>
> You meant Comparable, right? There's no connection between Enumerable
> and #<=> or #succ.


You are correct. Not enough sleep this weekend.

 
Reply With Quote
 
Michael Linfield
Guest
Posts: n/a
 
      09-03-2007
> but then how would i match that against the original dates for the data?
> match = original & result


now as far as matching that date to grep'd data should i use a string
comparison or is there a better way?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract a range i.e. svr[100..130] ? Richard Sandoval Ruby 12 04-18-2011 08:33 PM
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
range() is not the best way to check range? Summercoolness@gmail.com Python 46 07-25-2006 08:10 PM
Extract range of lines from a text file Amer Neely Perl Misc 29 04-10-2006 12:28 AM
extract range of lines using range op bug? it_says_BALLS_on_your forehead Perl Misc 3 03-03-2006 04:28 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57