Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > filling an array excepted first and last position...

Reply
Thread Tools

filling an array excepted first and last position...

 
 
Josselin
Guest
Posts: n/a
 
      10-21-2006
On 2006-10-20 13:40:44 +0200, Robert Klemme <(E-Mail Removed)> said:

> On 20.10.2006 13:35, Josselin wrote:
>> On 2006-10-20 10:32:18 +0200, Robert Klemme <(E-Mail Removed)> said:
>>
>>> On 20.10.2006 09:59, Josselin wrote:
>>>> ldom = 30 # variable (last day of a month...)
>>>>
>>>> # array init to 0
>>>> @obk = []
>>>> 1.step(ldom, 1) do |i|
>>>> @obk[i] = 0
>>>> end
>>>
>>> @obk = Array.new(ldom, 0)

>> better , avoiding @obk[0] = nil ...
>>>
>>>> I am filling an array as follows (part of a larger filling scheme) :
>>>> ----
>>>> #fiiling the array upon another array content
>>>> # but I'd like to AVOID filling the first and last position in this loop....
>>>> obdays.each do |bd|
>>>> @obk[bd] = 1
>>>> end
>>>> ------
>>>> # ajusting first and last position (in the larger scheme)
>>>> @obk[obdays.first] += -1
>>>> @obk[obdays.last] += -2
>>>
>>> I am not exactly sure what problem you have with this approach. Here
>>> is a different one
>>>
>>> obdays.each_with_index do |bd, idx|
>>> case idx
>>> when 0
>>> # ignore, @obk[bd] is 0 already
>>> when obdays.length - 1
>>> # set to 1 + (-2) = -1
>>> @obk[bd] = -1
>>> else
>>> @obk[bd] = 1
>>> end
>>> end
>>>
>>> Does that help?
>>>
>>> robert

>>
>> yes and no, I cannot test @obk[bd] == 0 as it may change.... as
>> indicated in one of my response post

>
> ??? There is no test for @obk[bd] == 0 in the code above. It's a bit
> difficult to guess what you really want with the information I have
> seen.
>
> Cheers
>
> robert


Thanks Robert, I solved the problem creating a sub-range without the
first and last position of the the initial range

other_booking_days = (bk.start_at.day..bk.end_at.day)

@obk[other_booking_days.first] += -1
obd = (bk.start_at.day+1..bk.end_at.day-1)
obd.each {|bd| @obk[bd] = 1 }
@obk[other_booking_days.last] += -2

all range is initialized to 0
first and last position in the range act like incremental counters..
in between just get a value




 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      10-21-2006
Josselin wrote:
> all range is initialized to 0
> first and last position in the range act like incremental counters..
> in between just get a value


This sounds as if the design is at least questionable. If you have
separate counters then why not make them separate? Do they have
anything in common with all the other values in the array?

Regards

robert
 
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
identifier excepted minie Java 1 06-27-2008 02:23 PM
Add Array#first= and Array#last= to std lib Robert Klemme Ruby 35 12-31-2007 03:00 PM
how common is it to get email spam with your first, first and last and/or home town in it? Lookout Computer Support 16 04-27-2006 07:42 AM
Variable width menu items + fixed total size + filling last element Andreas Pardeike HTML 7 08-19-2004 02:50 PM
Dynamically filling in a 3rd value in select pulldowns depending on whats entered in the first 2 J. Muenchbourg Javascript 1 07-29-2003 11:58 AM



Advertisments