[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

creating dynamic hashmap

Sameera Gayan

7/4/2008 5:03:00 AM

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
--
Posted via http://www.ruby-....

5 Answers

Robert Klemme

7/4/2008 6:41:00 AM

0

On 04.07.2008 07:03, Sameera Gayan wrote:
> 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...

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

Sameera Gayan

7/4/2008 8:08:00 AM

0

Robert Klemme wrote:
> On 04.07.2008 07:03, Sameera Gayan 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




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

Robert Klemme

7/4/2008 2:51:00 PM

0

On 04.07.2008 10:07, Sameera Gayan wrote:
> Robert Klemme wrote:
>> On 04.07.2008 07:03, Sameera Gayan 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.

> 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 ..

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.ht...

Cheers

robert

Siep Korteling

7/6/2008 11:18:00 PM

0

Sameera Gayan wrote:
> Robert Klemme wrote:
>> On 04.07.2008 07:03, Sameera Gayan 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

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

Sameera Gayan

7/7/2008 2:47:00 AM

0

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





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