using step backward ?

Discussion in 'Ruby' started by Josselin, Feb 24, 2007.

  1. Josselin

    Josselin Guest

    I have an array myArray = ["22", "31", "56", "89", "47"]

    I would like to produce a string like : "22#31#56#89>47<" where the
    last item must be : >last_item<

    case 1 item : ["22"] => ">22<"
    case 2 items : ["22", "31"] => "22#>31<"
    ...
    case n items : ["22", "31", "56", .........., "89", "47"] =>
    "22#31#56#...........#89>47<"

    as it's always the last items getting special treatment I thought
    starting with " >"+myArray.last +"< " then
    using 1.step(myArray.length-1, -1) do {..... put # after item } and
    concatenate if myArray.length > 1
    but I cannot get it right... I cannot get simple

    tfyh

    joss
     
    Josselin, Feb 24, 2007
    #1
    1. Advertising

  2. Le samedi 24 f=E9vrier 2007 19:00, Josselin a =E9crit=A0:
    > I have an array myArray =3D ["22", "31", "56", "89", "47"]
    >
    > I would like to produce a string like : "22#31#56#89>47<" where the
    > last item must be : >last_item<
    >
    > case 1 item : ["22"] =3D> ">22<"
    > case 2 items : ["22", "31"] =3D> "22#>31<"
    > ..
    > case n items : ["22", "31", "56", .........., "89", "47"] =3D>
    > "22#31#56#...........#89>47<"
    >
    > as it's always the last items getting special treatment I thought
    > starting with " >"+myArray.last +"< " then
    > using 1.step(myArray.length-1, -1) do {..... put # after item } and
    > concatenate if myArray.length > 1
    > but I cannot get it right... I cannot get simple
    >
    > tfyh
    >
    > joss


    You may want to use Array#slice and Array#join to do that. And, as you said=
    ,=20
    make a special treatment for the last element.

    =2D-=20
    Olivier Renaud
     
    Olivier Renaud, Feb 24, 2007
    #2
    1. Advertising

  3.  
    Rob Biedenharn, Feb 24, 2007
    #3
  4. Josselin

    Phrogz Guest

    On Feb 24, 10:59 am, Josselin <> wrote:
    > I have an array myArray = ["22", "31", "56", "89", "47"]
    >
    > I would like to produce a string like : "22#31#56#89>47<" where the
    > last item must be : >last_item<


    irb(main):001:0> myArray = ["22", "31", "56", "89", "47"]
    => ["22", "31", "56", "89", "47"]
    irb(main):002:0> last = myArray.pop
    => "47"
    irb(main):003:0> myArray.join('#')+">#{last}<"
    => "22#31#56#89>47<"
     
    Phrogz, Feb 24, 2007
    #4
  5. Josselin

    Josselin Guest

    On 2007-02-24 19:51:16 +0100, "Phrogz" <> said:

    > On Feb 24, 10:59 am, Josselin <> wrote:
    >> I have an array myArray = ["22", "31", "56", "89", "47"]
    >>
    >> I would like to produce a string like : "22#31#56#89>47<" where the
    >> last item must be : >last_item<

    >
    > irb(main):001:0> myArray = ["22", "31", "56", "89", "47"]
    > => ["22", "31", "56", "89", "47"]
    > irb(main):002:0> last = myArray.pop
    > => "47"
    > irb(main):003:0> myArray.join('#')+">#{last}<"
    > => "22#31#56#89>47<"


    thanks it runs well for 1or n > 2 items but not for 2 items

    myArray = ["22", "31"]
    => ["22", "31"]
    irb(main):002:0> last = myArray.pop
    => "31"
    irb(main):003:0> myArray.join('#')+">#{last}<"
    => "22>31<"

    I should get "22#>31<", the # must come after each item excepted the last..
     
    Josselin, Feb 24, 2007
    #5
  6. Josselin

    Raj Sahae Guest

    Josselin wrote:
    >>> I have an array myArray = ["22", "31", "56", "89", "47"]
    >>>
    >>> I would like to produce a string like : "22#31#56#89>47<" where the
    >>> last item must be : >last_item<

    Here you say that you want the second to last item to have no # after it,
    >
    > I should get "22#>31<", the # must come after each item excepted the
    > last..


    but here you say you want a # after it.
    If you want no # sign, then Josselin's solution works.
    If you want a # sign, just push that last item back on the array and
    join it with #

    myArray = ["22", "31", "56", "89", "47"]
    myArray2 = ["22", "47"]
    myArray3 = ["47"]

    arrays = [myArray, myArray2, myArray3]

    arrays.each{|array| puts array.push('>' + array.pop.to_s + '<').join('#')}
     
    Raj Sahae, Feb 24, 2007
    #6
  7. Josselin

    Gary Wright Guest

    On Feb 24, 2007, at 1:00 PM, Josselin wrote:

    > I have an array myArray = ["22", "31", "56", "89", "47"]
    >
    > I would like to produce a string like : "22#31#56#89>47<" where
    > the last item must be : >last_item<
    >
    > case 1 item : ["22"] => ">22<"
    > case 2 items : ["22", "31"] => "22#>31<"
    > ..
    > case n items : ["22", "31", "56", .........., "89", "47"] =>
    > "22#31#56#...........#89>47<"


    I'm assuming you have a typo in that last case and want the
    string to end with: "#89#>47<". It isn't clear how you would
    want an empty list formatted but here is one possibility:

    $ cat format.rb

    def format(list)
    list[0..-2].push(">#{list.last}<").join('#')
    end

    puts format([])
    puts format(%w{22})
    puts format(%w{22 31})
    puts format(%w{22 31 56})

    $ ruby format.rb
    ><
    >22<

    22#>31<
    22#31#>56<




    Gary Wright
     
    Gary Wright, Feb 24, 2007
    #7
  8. Josselin

    Josselin Guest

    On 2007-02-24 21:50:18 +0100, Gary Wright <> said:

    >
    > On Feb 24, 2007, at 1:00 PM, Josselin wrote:
    >
    >> I have an array myArray = ["22", "31", "56", "89", "47"]
    >>
    >> I would like to produce a string like : "22#31#56#89>47<" where
    >> the last item must be : >last_item<
    >>
    >> case 1 item : ["22"] => ">22<"
    >> case 2 items : ["22", "31"] => "22#>31<"
    >> ..
    >> case n items : ["22", "31", "56", .........., "89", "47"] =>
    >> "22#31#56#...........#89>47<"

    >
    > I'm assuming you have a typo in that last case and want the
    > string to end with: "#89#>47<".


    YES .. sorry
    > It isn't clear how you would
    > want an empty list formatted but here is one possibility:
    >
    > $ cat format.rb
    >
    > def format(list)
    > list[0..-2].push(">#{list.last}<").join('#')
    > end
    >
    > puts format([])
    > puts format(%w{22})
    > puts format(%w{22 31})
    > puts format(%w{22 31 56})
    >
    > $ ruby format.rb
    > ><
    > >22<

    > 22#>31<
    > 22#31#>56<


    THAT's it......, very useful.. I put '#' and '>' '<' charcaters just to
    make it simple.. but the objective is to insert html tags
    to format correctly an output string...
    >
    >
    >
    > Gary Wright
     
    Josselin, Feb 24, 2007
    #8
    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. jaap de verwant slachter
    Replies:
    0
    Views:
    1,275
    jaap de verwant slachter
    Jul 1, 2003
  2. Roy in

    need step by step example

    Roy in, Aug 3, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    363
    Roy in
    Aug 3, 2003
  3. Steve Richter

    a step by step page

    Steve Richter, May 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    382
    Steve Richter
    May 3, 2005
  4. Lakota2k
    Replies:
    0
    Views:
    133
    Lakota2k
    Apr 14, 2005
  5. anne001
    Replies:
    7
    Views:
    169
    Logan Capaldo
    Nov 21, 2005
Loading...

Share This Page