Gsub problem

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,

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]

any help will be appreciated.
thank you

···

--
Posted via http://www.ruby-forum.com/.

$ irb
irb(main):001:0> str="[1,2][2,1][3,1][4,1],"
=> "[1,2][2,1][3,1][4,1],"
irb(main):002:0> str.gsub(/\]\[/, '],[').sub(/,$/, '')
=> "[1,2],[2,1],[3,1],[4,1]"

···

--
Kent

On 3/2/06, huseyin polat <huseyinpolat@gmail.com> 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,

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]

any help will be appreciated.
thank you

--
Posted via http://www.ruby-forum.com/\.

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

···

On Fri, 3 Mar 2006, huseyin polat wrote:

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

Please try these.

1. str.gsub(/\]\[/, '],[')

2. str.sub(/,$/, '')

Best regards,

JS

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,

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]

any help will be appreciated.
thank you

irb(main):035:0* str="[1,2][2,1][3,1][4,1]"
=> "[1,2][2,1][3,1][4,1]"
irb(main):036:0> str.gsub '][', '],['
=> "[1,2],[2,1],[3,1],[4,1]"

irb(main):037:0> str += ','
=> "[1,2][2,1][3,1][4,1],"
irb(main):038:0> str.chomp ','
=> "[1,2][2,1][3,1][4,1]"

Jim

first problem:

puts "[1,2][2,1][3,1][4,1]".gsub(/\]\[/, '],[')

second problem: headache, im a noob on regexp

huseyin polat schrieb:

···

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,

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]

any help will be appreciated.
thank you

huseyin polat wrote:

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,

no need of regexps nor gsub:

str.split("][").join("],[") #=> "[1,2],[2,1],[3,1],[4,1]"

Ciao!
Marco

···

--
This way, to star trek...

--
Posted via http://www.ruby-forum.com/\.

(first problem)

Other people's solution should work, but you don't really need to use regex for this one.

try this:

str = "[1,2][2,1][3,1][4,1]"
str.gsub!('][', '],[')

(second problem)

str = "[1, 2][2, 1][3, 1][4, 1],"
str.gsub!(/,$/, '')

cheers,
daesan

···

On Mar 3, 2006, at 1:13 AM, 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,

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]

any help will be appreciated.
thank you

--
Posted via http://www.ruby-forum.com/\.

Kent Sibilev wrote:

$ irb
irb(main):001:0> str="[1,2][2,1][3,1][4,1],"
=> "[1,2][2,1][3,1][4,1],"
irb(main):002:0> str.gsub(/\]\[/, '],[').sub(/,$/, '')
=> "[1,2],[2,1],[3,1],[4,1]"

--
Kent

Kent thank you very much,But I did that and I got
[12][2,1][3,1][4,1] it got rid of last comma but also got rid of the
first comma between 1 and 2 and print them out like 12
any clue?
thank you

···

--
Posted via http://www.ruby-forum.com/\.

Kent Sibilev wrote:

$ irb
irb(main):001:0> str="[1,2][2,1][3,1][4,1],"
=> "[1,2][2,1][3,1][4,1],"
irb(main):002:0> str.gsub(/\]\[/, '],[').sub(/,$/, '')
=> "[1,2],[2,1],[3,1],[4,1]"

--
Kent

Kent
ignore my first message please, yeah I have tried that but program still
prints out as:
[12][21][31][41]

no commas :frowning:

···

--
Posted via http://www.ruby-forum.com/\.

unknown wrote:

···

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

thank you, however your first solution didn't make any difference and
the second got rid of all the commas. thank you anyway.
I have tried your second comments as
str.gsub(%r/,+$/,"").gsub(%r/\]\[/, "],[")
I don't know why.
thank you for trying

--
Posted via http://www.ruby-forum.com/\.

Srinivas Jonnalagadda wrote:

Please try these.

1. str.gsub(/\]\[/, '],[')

2. str.sub(/,$/, '')

Best regards,

JS

thank you JS, however it didn'r change anything.
I am still getting output as
[1,2][2,1][3,1][4,1]

···

--
Posted via http://www.ruby-forum.com/\.

bluemonk wrote:

huseyin polat wrote:

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,

no need of regexps nor gsub:

str.split("][").join("],[") #=> "[1,2],[2,1],[3,1],[4,1]"

Ciao!
Marco

--
This way, to star trek...

thank for post, but didn't change anything. maybe I should post my
entire program.
anybody wants to post their email to take a look at the program ?
one more thing, I get the string from a value [1,2][2,1][3,1][4,1] that
is originally created by push an array and then copied into my hash

bighash.sort.each{|key,value|
   value.each{ |value|
    str=String.new
    value=value.to_s
    str=value
  print str.split("][").join("],[")
  }

···

--
Posted via http://www.ruby-forum.com/\.

huseyin polat wrote:

thank for post, but didn't change anything. maybe I should post my entire program.
anybody wants to post their email to take a look at the program ?
one more thing, I get the string from a value [1,2][2,1][3,1][4,1] that is originally created by push an array and then copied into my hash

bighash.sort.each{|key,value|
   value.each{ |value|
    str=String.new
    value=value.to_s
    str=value
  print str.split("][").join("],[")
  }

Here's the output I get when I try Marco's solution. It looks right to me.

iptc$ irb
irb(main):001:0> str="[1,2][2,1][3,1][4,1]"
=> "[1,2][2,1][3,1][4,1]"
irb(main):002:0> puts str.split("][").join("],[")
[1,2],[2,1],[3,1],[4,1]
=> nil
irb(main):003:0>

Several of the solutions posted should work. Time for a sanity check:
Are your sure that the _input_ is what you think it is?

Jim

irb(main):001:0> "[1,2][2,1][3,1][4,1]".gsub(/\]\[/, '],[')
=> "[1,2],[2,1],[3,1],[4,1]"

···

On 2-Mar-06, at 7:28 PM, Tim Hunter wrote:

huseyin polat wrote:

thank for post, but didn't change anything. maybe I should post my entire program.
anybody wants to post their email to take a look at the program ?
one more thing, I get the string from a value [1,2][2,1][3,1][4,1] that is originally created by push an array and then copied into my hash
bighash.sort.each{|key,value|
   value.each{ |value|
    str=String.new
    value=value.to_s
    str=value
  print str.split("][").join("],[")
  }

Here's the output I get when I try Marco's solution. It looks right to me.

iptc$ irb
irb(main):001:0> str="[1,2][2,1][3,1][4,1]"
=> "[1,2][2,1][3,1][4,1]"
irb(main):002:0> puts str.split("][").join("],[")
[1,2],[2,1],[3,1],[4,1]
=> nil
irb(main):003:0>

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

Dear Friends, thank you very much you all Jim, daesan, Mike, Timothy,
Marco, Gregor, Srinivas, Kent and Unknown guests.
all of you guys' solution was correct and here what I did wrong in
previous section of my code.
          bighash[key]= bighash[key].push(arrtemp)
and this was putting into my hash some random numbers and at the end
when I try to print it out whole value, it was printing fine, like
[12][42][32] but when I try to print value[0] it was printing random
number and this wasn't easy to catch since I was printing all the values
with bracets..
as soon as I have added bighash[key] = Array.new() initialization,
          bighash[key] = Array.new()
          bighash[key]= bighash[key].push(arrtemp)
all the solutions you guys posted worked out.
sorry for the headache and thank you for the support, this forum is
perfect, I will spread all my friends who are taking search engine class
with me.
thank you and have a nice day.

···

--
Posted via http://www.ruby-forum.com/.