filling an array excepted first and last position...

Discussion in 'Ruby' started by Josselin, Oct 20, 2006.

  1. Josselin

    Josselin Guest

    ldom = 30 # variable (last day of a month...)

    # array init to 0
    @obk = []
    1.step(ldom, 1) do |i|
    @obk = 0
    end

    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
     
    Josselin, Oct 20, 2006
    #1
    1. Advertising

  2. > ldom = 30 # variable (last day of a month...)
    >
    > # array init to 0
    > @obk = []
    > 1.step(ldom, 1) do |i|
    > @obk = 0
    > end


    Why not:

    @obk = Array.new(ldom,0)


    > 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

    <...>

    Not sure what are you trying to do here, but maybe something like this
    may suit you?

    @obk.fill(1,ldom-1){|i| obdays}


    Regards,
    Rimantas
    --
    http://rimantas.com/
     
    Rimantas Liubertas, Oct 20, 2006
    #2
    1. Advertising

  3. Josselin

    Peter Szinek Guest

    Josselin wrote:
    > ldom = 30 # variable (last day of a month...)
    >
    > # array init to 0
    > @obk = []
    > 1.step(ldom, 1) do |i|
    > @obk = 0
    > end


    You could do also
    @obk = Array.new(ldom) { 0 }
    >
    > 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


    hmm... there are maybe better ways for this, but I would try:

    (-2).each {|i| puts @obk }

    HTH,
    Peter
    http://www.rubyrailways.com
     
    Peter Szinek, Oct 20, 2006
    #3
  4. 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 = 0
    > end


    @obk = Array.new(ldom, 0)

    > 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
     
    Robert Klemme, Oct 20, 2006
    #4
  5. On 10/20/06, Josselin <> wrote:
    > ldom = 30 # variable (last day of a month...)
    >
    > # array init to 0
    > @obk = []
    > 1.step(ldom, 1) do |i|
    > @obk = 0
    > end
    >
    > 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


    How about:
    @obk = Array.new(ldom, 0)
    obdays[1..-2].to_a.each{|bd| @obk[bd] = 1}

    to_a just in case obdays is empty, in which case obdays[1..-2] return nil.
    I don't know why Array#[] with a range cannot just always return [].

    And don't worry about creating a new object,
    obdays and obdays[1..-2] actually share storage.

    --
    Tomasz Wegrzanowski [ http://t-a-w.blogspot.com/ ]
     
    Tomasz Wegrzanowski, Oct 20, 2006
    #5
  6. Josselin

    Josselin Guest

    On 2006-10-20 09:59:58 +0200, Josselin <> said:

    > ldom = 30 # variable (last day of a month...)
    >
    > # array init to 0
    > @obk = []
    > 1.step(ldom, 1) do |i|
    > @obk = 0
    > end
    >
    > 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


    thanks to all of you, i'll try all your clues...
    Ruby is incredible... you can writing as in any C, C++, java multi
    lines routine.. then shrink it to a minimal line... that's why newbie
    ask for well-trained support at first then may require guru help !!
     
    Josselin, Oct 20, 2006
    #6
  7. Josselin

    Josselin Guest

    On 2006-10-20 10:26:50 +0200, "Rimantas Liubertas" <> said:

    >> ldom = 30 # variable (last day of a month...)
    >>
    >> # array init to 0
    >> @obk = []
    >> 1.step(ldom, 1) do |i|
    >> @obk = 0
    >> end

    >
    > Why not:
    >
    > @obk = Array.new(ldom,0)


    yes , it's better .... avoiding @obk[0] = nil in my writing...
    >
    >
    >> 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

    > <...>
    >
    > Not sure what are you trying to do here, but maybe something like this
    > may suit you?
    >
    > @obk.fill(1,ldom-1){|i| obdays}

    obdays are all days in a given period in a month (ex : 2 to 6 november)
    I need to fill @obk[3] = 1 ...... @obk[5] = 1 # booked
    but @obk[2] += -1 (in value) @obk[6] += -2 (out value)

    why adding these values ?
    because on november 6, I can also have late another booking period starting

    so I will get
    @obk[6] += 1 (in value)... in which case @obk[6] will be -3

    finally I got my array for november from 1 to 30
    Day 1 2 3 4 5 6 7 8 9 ......
    value 0 -1 1 1 1 -3 1 1 1 .....
    these values are used in a javascript calendar to fix the CSS class for display
    0 no booking color
    -1 half day starting
    1 booked day
    -3 half day ending / half day starting
    ...
    -2 half day ending

    that's it....

    >
    >
    > Regards,
    > Rimantas
     
    Josselin, Oct 20, 2006
    #7
  8. Josselin

    Josselin Guest

    On 2006-10-20 10:32:18 +0200, Robert Klemme <> 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 = 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
     
    Josselin, Oct 20, 2006
    #8
  9. On 20.10.2006 13:35, Josselin wrote:
    > On 2006-10-20 10:32:18 +0200, Robert Klemme <>
    > 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 = 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
     
    Robert Klemme, Oct 20, 2006
    #9
  10. Josselin

    Josselin Guest

    On 2006-10-20 10:54:24 +0200, "Tomasz Wegrzanowski"
    <> said:

    > On 10/20/06, Josselin <> wrote:
    >> ldom = 30 # variable (last day of a month...)
    >>
    >> # array init to 0
    >> @obk = []
    >> 1.step(ldom, 1) do |i|
    >> @obk = 0
    >> end
    >>
    >> 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

    >
    > How about:
    > @obk = Array.new(ldom, 0)
    > obdays[1..-2].to_a.each{|bd| @obk[bd] = 1}


    interesting but when I enter this command obdays is already a range :

    > obdays = > 2..6

    so I want to do someting like that :
    obdays[3..5].to_a.each{|bd| @obk[bd] = 1}
    starting the position after the first in the range and ending with the
    position before the last
    like obdays[start+1..end-1].to_a.each{|bd| @obk[bd] = 1}
    ?

    >
    > to_a just in case obdays is empty, in which case obdays[1..-2] return nil.
    > I don't know why Array#[] with a range cannot just always return [].
    >
    > And don't worry about creating a new object,
    > obdays and obdays[1..-2] actually share storage.
     
    Josselin, Oct 20, 2006
    #10
  11. Josselin

    Josselin Guest

    On 2006-10-20 13:40:44 +0200, Robert Klemme <> said:

    > On 20.10.2006 13:35, Josselin wrote:
    >> On 2006-10-20 10:32:18 +0200, Robert Klemme <> 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 = 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
     
    Josselin, Oct 21, 2006
    #11
  12. 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
     
    Robert Klemme, Oct 21, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. minie

    identifier excepted

    minie, Jun 27, 2008, in forum: Java
    Replies:
    1
    Views:
    384
    minie
    Jun 27, 2008
  2. Robert Klemme
    Replies:
    35
    Views:
    322
    Robert Klemme
    Dec 31, 2007
  3. Chris R.
    Replies:
    3
    Views:
    142
    Adam Prescott
    Jan 28, 2011
  4. Mohsen Pahlevanzadeh
    Replies:
    0
    Views:
    124
    Mohsen Pahlevanzadeh
    Sep 9, 2013
  5. MRAB
    Replies:
    0
    Views:
    123
Loading...

Share This Page