Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Need assistance with hash of arrays and injecting values

Reply
Thread Tools

Need assistance with hash of arrays and injecting values

 
 
Alpha Blue
Guest
Posts: n/a
 
      09-25-2010
I'm creating a class that will run a number of tasks based on a start
and end date. It only runs between week 4 and week 15 of my current
system.

A current hash of arrays is defined for the current calendar year the
tasks run.

@h = {
:week_four => ["2010-09-20", "2010-09-26"],
:week_five => ["2010-09-27", "2010-10-03"],
:week_six => ["2010-10-04", "2010-10-10"],
:week_seven => ["2010-10-11", "2010-10-17"],
:week_eight => ["2010-10-18", "2010-10-24"],
:week_nine => ["2010-10-25", "2010-10-31"],
:week_ten => ["2010-11-01", "2010-11-07"],
:week_eleven => ["2010-11-08", "2010-11-14"],
:week_twelve => ["2010-11-15", "2010-11-21"],
:week_thirteen => ["2010-11-22", "2010-11-28"],
:week_fourteen => ["2010-11-29", "2010-12-05"],
:week_fifteen => ["2010-12-06", "2010-12-12"]
}

I want to inject these into a similar task:

@current_week is an instance variable within the class that holds the
current week (i.e. week 10 for instance so '10')

@weekly_report is defined as an array that will hold the report
listings.

The VirtualReport.reportlistings method takes two dates - a start date
and an end date. I only want them to run up to the current week. So,
in this example, if it's week 10, it should run a report for week
4,5,6,7,8,9, and 10. But, it should not run a report for
11,12,13,14,15. Furthermore, it should check to ensure that the data is
not empty.

for i in 0..(@current_week-4)
@weekly_report = VirtualReport.reportlistings("INSERT HASH VALUES")
end

Where I placed the INSERT HASH VALUES above is where I want to inject my
values based on the current week. Therefore, I'm pretty certain the
hash needs to be ordered or sorted so that they maintain the current
week layouts. But, I'm not very good with hashes in general and could
use some help or a better example of how to accomplish this task. The
weekly reports will be stored in an array and used later on in the
class.

Any help would be appreciated.

Thanks,

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

 
Reply With Quote
 
 
 
 
Alpha Blue
Guest
Posts: n/a
 
      09-25-2010
As an example of what the method looks like when run properly:

@weekly_report = VirtualReport.reportlistings("2010-09-20", "2010-09-26)
etc..

Thanks.

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

 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      09-25-2010
On Sat, Sep 25, 2010 at 2:29 PM, Alpha Blue <> wrote:
> I'm creating a class that will run a number of tasks based on a start
> and end date. =A0It only runs between week 4 and week 15 of my current
> system.
>
> A current hash of arrays is defined for the current calendar year the
> tasks run.
>
> @h =3D {
> =A0:week_four =3D> ["2010-09-20", "2010-09-26"],
> =A0:week_five =3D> ["2010-09-27", "2010-10-03"],
> =A0:week_six =3D> ["2010-10-04", "2010-10-10"],
> =A0:week_seven =3D> ["2010-10-11", "2010-10-17"],
> =A0:week_eight =3D> ["2010-10-18", "2010-10-24"],
> =A0:week_nine =3D> ["2010-10-25", "2010-10-31"],
> =A0:week_ten =3D> ["2010-11-01", "2010-11-07"],
> =A0:week_eleven =3D> ["2010-11-08", "2010-11-14"],
> =A0:week_twelve =3D> ["2010-11-15", "2010-11-21"],
> =A0:week_thirteen =3D> ["2010-11-22", "2010-11-28"],
> =A0:week_fourteen =3D> ["2010-11-29", "2010-12-05"],
> =A0:week_fifteen =3D> ["2010-12-06", "2010-12-12"]
> }
>
> I want to inject these into a similar task:
>
> @current_week is an instance variable within the class that holds the
> current week (i.e. week 10 for instance so '10')
>
> @weekly_report is defined as an array that will hold the report
> listings.
>
> The VirtualReport.reportlistings method takes two dates - a start date
> and an end date. =A0I only want them to run up to the current week. =A0So=

,
> in this example, if it's week 10, it should run a report for week
> 4,5,6,7,8,9, and 10. =A0But, it should not run a report for
> 11,12,13,14,15. =A0Furthermore, it should check to ensure that the data i=

s
> not empty.
>
> for i in 0..(@current_week-4)
> =A0@weekly_report =3D VirtualReport.reportlistings("INSERT HASH VALUES")
> end
>
> Where I placed the INSERT HASH VALUES above is where I want to inject my
> values based on the current week. =A0Therefore, I'm pretty certain the
> hash needs to be ordered or sorted so that they maintain the current
> week layouts. =A0But, I'm not very good with hashes in general and could
> use some help or a better example of how to accomplish this task. =A0The
> weekly reports will be stored in an array and used later on in the
> class.
>
> Any help would be appreciated.


If I understood correctly, you want to get for values of i
4,5,6,7,8,9,10 the arrays corresponding to the key :week_<week>,
so, first, you need to translate from the number to the word (4 =3D>
four). The easiest way would be to change your @h array, if possible:

@h =3D {
4 =3D> ["2010-09-20", "2010-09-26"],
5 =3D> ["2010-09-27", "2010-10-03"],
...
}

If this is not possible, I would build a translation hash for that:

TRANSLATION =3D {4 =3D> :week_four, 5 =3D> :week_five, ...}

and do:

4.upto(@current_week) do |week|
weekly_report =3D VirtualReport.reportlistings(@h[TRANSLATION[i]])
# do something with the report
end

Hope this helps,

Jesus.

 
Reply With Quote
 
Alpha Blue
Guest
Posts: n/a
 
      09-26-2010
Thanks Jesus, that works perfect and is much more simplified.

4.upto(@current_week) do |week|
@weekly_report = VirtualReport.reportlistings(@h[week][0],@h[week][1])
p "#{@h[week][0]} and #{@h[week][1]}"
end



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

 
Reply With Quote
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      09-26-2010
On Sun, Sep 26, 2010 at 5:27 PM, Alpha Blue <> wrote:
> Thanks Jesus, that works perfect and is much more simplified.
>
> 4.upto(@current_week) do |week|
> =A0@weekly_report =3D VirtualReport.reportlistings(@h[week][0],@h[week][1=

])
> =A0p "#{@h[week][0]} and #{@h[week][1]}"
> end


You could also do:

...
weekly_report =3D VirtualReport.reportlistings(*@h[week])
...

Jesus.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      09-27-2010
On Sat, Sep 25, 2010 at 2:29 PM, Alpha Blue <> wrote:
> I'm creating a class that will run a number of tasks based on a start
> and end date. =A0It only runs between week 4 and week 15 of my current
> system.
>
> A current hash of arrays is defined for the current calendar year the
> tasks run.
>
> @h =3D {
> =A0:week_four =3D> ["2010-09-20", "2010-09-26"],
> =A0:week_five =3D> ["2010-09-27", "2010-10-03"],
> =A0:week_six =3D> ["2010-10-04", "2010-10-10"],
> =A0:week_seven =3D> ["2010-10-11", "2010-10-17"],
> =A0:week_eight =3D> ["2010-10-18", "2010-10-24"],
> =A0:week_nine =3D> ["2010-10-25", "2010-10-31"],
> =A0:week_ten =3D> ["2010-11-01", "2010-11-07"],
> =A0:week_eleven =3D> ["2010-11-08", "2010-11-14"],
> =A0:week_twelve =3D> ["2010-11-15", "2010-11-21"],
> =A0:week_thirteen =3D> ["2010-11-22", "2010-11-28"],
> =A0:week_fourteen =3D> ["2010-11-29", "2010-12-05"],
> =A0:week_fifteen =3D> ["2010-12-06", "2010-12-12"]
> }


I don't see the point in defining this. There is class Date which
abstracts dates and allows to do calculations on them. Also, encoding
something which is numeric (the week number) as text in a Hash key
seems overly awkward. This makes calculations of the kind "is date x
in week y" overly complicated.

> I want to inject these into a similar task:
>
> @current_week is an instance variable within the class that holds the
> current week (i.e. week 10 for instance so '10')
>
> @weekly_report is defined as an array that will hold the report
> listings.
>
> The VirtualReport.reportlistings method takes two dates - a start date
> and an end date. =A0I only want them to run up to the current week. =A0So=

,
> in this example, if it's week 10, it should run a report for week
> 4,5,6,7,8,9, and 10. =A0But, it should not run a report for
> 11,12,13,14,15. =A0Furthermore, it should check to ensure that the data i=

s
> not empty.
>
> for i in 0..(@current_week-4)
> =A0@weekly_report =3D VirtualReport.reportlistings("INSERT HASH VALUES")
> end
>
> Where I placed the INSERT HASH VALUES above is where I want to inject my
> values based on the current week. =A0Therefore, I'm pretty certain the
> hash needs to be ordered or sorted so that they maintain the current
> week layouts. =A0But, I'm not very good with hashes in general and could
> use some help or a better example of how to accomplish this task. =A0The
> weekly reports will be stored in an array and used later on in the
> class.
>
> Any help would be appreciated.


I'd start with a proper representation of dates (class Date comes to
mind) and add another one for representing weeks on top of that. So
you could do

# untested
Week =3D Struct.new :start_date do
def initialize(date)
self.start_date =3D find_start_date(date)
end

def in_week?(date)
(start_date ... (start_date + 7)).include? date
end

def week_of_year
first =3D find_start_date(Date.new(start_date.year, 1, 1))
(start_date - first) / 7 + 1
end

include Comparable

def <=3D>(week)
star_date <=3D> week.start_date
end

def succ
self.class.new(start_date + 7)
end

def pred
self.class.new(start_date - 7)
end

private
def find_start_date(date)
until date.monday? # or whatever should start your week
date =3D date.prev_day
end
date
end
end

Then base your other data structures on these primitive types.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Alpha Blue
Guest
Posts: n/a
 
      09-27-2010
>
> Then base your other data structures on these primitive types.
>
> Kind regards
>
> robert


Hi Robert, I'll have to mull over everything you just posted.
Eventually, I would like to clean up a lot of my date code. Originally
I had based the start and end weeks on the dates they were compiled_on
in the database. The dates in the database were simply YYYY-MM-DD
format. When I compiled data, I updated the compiled_on dates and when
I further tasked specific database tables for calculation entries, I
looked within the compiled_on date parameters.

This in turn has not made for a clean way of working with things over
time in a modular fashion. If, for example, I place 10 years worth of
data, it's difficult and messy to create yearly hashes. This is partly
why I wanted to start optimizing and cleaning up my code and then work
more concisely within a class.

Again, I'll look over your example and see what I can do.

--
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
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
hash of arrays - appending to one of the arrays Adam Akhtar Ruby 5 03-25-2008 11:53 PM
Need assistance with arrays RookThis Java 10 11-19-2007 08:57 AM
Sort a hash based on values in the hash stored as arrays of hashes Tore Aursand Perl Misc 3 09-16-2003 10:14 AM



Advertisments