On Fri, 3 Mar 2006, huseyin polat wrote:

> I have spend over 4 hours for this.. here is the question masters, very
> simple.
> str=String.new
> str="[1,2][2,1][3,1][4,1]"
>
> now I want to print this string as
> [1,2],[2,1],[3,1],[4,1]
> in other word I want to add comma between those ][ and make them look
> like ],[
> I tried gsub with hundred different possibilities but no luck,

harp:~ > ruby -e'  puts("[1,2][2,1][3,1][4,1]".gsub(%r/\]\[/, "],["))  '
[1,2],[2,1],[3,1],[4,1]


> second problem
> if my the same str has
> [1,2][2,1][3,1][4,1],
> and I want to get rid of last comma from the list, I tried chop chomp
> and all that but it gets ri of alot more than comma at the end. how can
> I get rid of last comma from the list? from [1,2][2,1][3,1][4,1], to
> [1,2][2,1][3,1][4,1]

harp:~ > ruby -e'  puts("[1,2][2,1][3,1][4,1],,,".gsub(%r/,+$/,"").gsub(%r/\]\[/, "],["))  '
[1,2],[2,1],[3,1],[4,1]

regards.

-a

-- 
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama