Creating dynamic hashmap

Hi All,

I have a question about creating a dynamic hash map. I’m using “gruff”
and in that , the lables parameter should be like this (as in example)

g.labels = { 0 => ‘Mon’, 2 => ‘Wed’, 4 => ‘Fri’, 6 => ‘Sun’ }

in my case, above dates are dynamic and user will select the date range.
So i need to create the hashmap dynamically.

can i use a loop, or … how can i do this,

any reply will greatly appriciated…

thankx guys,

cheers
sameera

On 04.07.2008 07:03, Sameera G. wrote:

any reply will greatly appriciated…
Can you be more specific about your problem? What kind of user input do
you expect? How do you want it to be translated into a Hash? etc.

robert

Robert K. wrote:

On 04.07.2008 07:03, Sameera G. wrote:

any reply will greatly appriciated…
Can you be more specific about your problem? What kind of user input do
you expect? How do you want it to be translated into a Hash? etc.

robert

Hi Robert,

Thank you for your reply,

My problem in like this,

I have a graph to display the crude oil price fluctuation with $. I’m
using “gruff” to generate graphs. This graph should generate for a given
date range.

In the x-axis of the graph i want to show the date range.

In ‘gruff’ it gets x-axis labels as a hash
Ex : g.labels = { 0 => ‘Mon’, 2 => ‘Wed’, 4 => ‘Fri’, 6 => ‘Sun’ }

So i want to dynamically create this hash map. Inside a loop. (so i can
loop the dates and create the x-axis depending on the users date range)

ultimate hash format should be { 0 => ‘Mon’, 2 => ‘Wed’, 4 => ‘Fri’, 6
=> ‘Sun’}

so that i can use it as

g.labels = hash_lables

hope i make myself clear. thankx for any reply …

cheers
sameera

On 04.07.2008 10:07, Sameera G. wrote:

Robert K. wrote:

On 04.07.2008 07:03, Sameera G. wrote:

any reply will greatly appriciated…
Can you be more specific about your problem? What kind of user input do
you expect? How do you want it to be translated into a Hash? etc.

g.labels = hash_lables

hope i make myself clear. thankx for any reply …

Um, this is pretty basic stuff. Did you actually use Hashes before?
You will need this to get your key value pairs defined:

http://www.ruby-doc.org/core/classes/Hash.html#M000161

Cheers

robert

Hi Siep,

Wow, U saved my day !!! that is what exactly i wanted, Still haven’t try
it (just now got in to office ;))

I will play around with it and hope it will fix my problem.

thankx a lot again for all Robert and You,

cheers
sameera

Sameera G. wrote:

Robert K. wrote:

On 04.07.2008 07:03, Sameera G. wrote:

any reply will greatly appriciated…
Can you be more specific about your problem? What kind of user input do
you expect? How do you want it to be translated into a Hash? etc.

robert

Hi Robert,

Thank you for your reply,

My problem in like this,

I have a graph to display the crude oil price fluctuation with $. I’m
using “gruff” to generate graphs. This graph should generate for a given
date range.

In the x-axis of the graph i want to show the date range.

In ‘gruff’ it gets x-axis labels as a hash
Ex : g.labels = { 0 => ‘Mon’, 2 => ‘Wed’, 4 => ‘Fri’, 6 => ‘Sun’ }

So i want to dynamically create this hash map. Inside a loop. (so i can
loop the dates and create the x-axis depending on the users date range)

ultimate hash format should be { 0 => ‘Mon’, 2 => ‘Wed’, 4 => ‘Fri’, 6
=> ‘Sun’}

so that i can use it as

g.labels = hash_lables

hope i make myself clear. thankx for any reply …

cheers
sameera

Is this what you need?

require ‘enumerator’
require ‘date’

print "enter start date: "
s = gets
print "enter end date: "
e = gets

start_date = Date.parse(s)
end_date = Date.parse(e)
dates = (start_date…end_date)

@slice_size = 2
@label_key = 0
@hash_labels = {}
dates.each_slice(@slice_size) do |date_slice|
@hash_labels[@label_key] = date_slice[0].strftime("%a")
@label_key += @slice_size
end

p @hash_labels

hth,

Siep