# Using step backward?

**URL:** <https://www.ruby-forum.com/t/using-step-backward/86696>\
**Category:** Ruby\
**Created:** [February 24, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/using-step-backward/86696 "2007-02-24T19:01:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 24, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/using-step-backward/86696/1 "2007-02-24T19:01:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 24, 2007, 7:18pm UTC](https://www.ruby-forum.com/t/using-step-backward/86696/2 "2007-02-24T19:18:01Z")

</div>

Le samedi 24 février 2007 19:00, Josselin a écrit :

> 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,  
make a special treatment for the last element.

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 24, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/using-step-backward/86696/3 "2007-02-24T19:56:06Z")

</div>

On Feb 24, 10:59 am, Josselin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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\<”

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 24, 2007, 8:06pm UTC](https://www.ruby-forum.com/t/using-step-backward/86696/4 "2007-02-24T20:06:07Z")

</div>

On 2007-02-24 19:51:16 +0100, “Phrogz” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 24, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/using-step-backward/86696/5 "2007-02-24T20:54:14Z")

</div>

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(’#’)}

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 25, 2007, 12:30am UTC](https://www.ruby-forum.com/t/using-step-backward/86696/6 "2007-02-25T00:30:43Z")

</div>

On 2007-02-24 21:50:18 +0100, Gary W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

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

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [February 24, 2007, 9:51pm UTC](https://www.ruby-forum.com/t/using-step-backward/86696/7 "2007-02-24T21:51:25Z")

</div>

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