[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

split a string at tab positions

suresh

5/30/2008 9:20:00 AM

Hi

I am a newbie to ruby and when i tried to split a string at tab
positions, it didnt work. Could you please tell me where I am going
wrong? Code is pasted.

#!/usr/bin/ruby1.9
s = "a b"
ss = s.split('\t')
puts ss[0]

It just prints the entire line without splitting...

suresh
6 Answers

botp

5/30/2008 9:27:00 AM

0

On Fri, May 30, 2008 at 5:19 PM, suresh <suresh.amritapuri@gmail.com> wrote:
> #!/usr/bin/ruby1.9
> s = "a b"
> ss = s.split('\t')
> puts ss[0]
> It just prints the entire line without splitting...

probably your editor is converting the tab to spaces.

try eg,

s="a\tb"
#=> "a\tb"
ss=s.split "\t"
#=> ["a", "b"]

kind regards -botp

Oscar Del Ben

5/30/2008 9:29:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

first, you should use double quote when you want to escape a character.

>> s = "a\tb"
=> "a\tb"
>> ss = s.split("\t")
=> ["a", "b"]



2008/5/30, suresh <suresh.amritapuri@gmail.com>:
>
> Hi
>
> I am a newbie to ruby and when i tried to split a string at tab
> positions, it didnt work. Could you please tell me where I am going
> wrong? Code is pasted.
>
> #!/usr/bin/ruby1.9
> s = "a b"
> ss = s.split('\t')
> puts ss[0]
>
> It just prints the entire line without splitting...
>
>
> suresh
>
>

suresh

5/30/2008 9:35:00 AM

0

On May 30, 5:28 am, Oscar Del Ben <thehcdrea...@gmail.com> wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> first, you should use double quote when you want to escape a character.
>
> >> s = "a\tb"
> => "a\tb"
> >> ss = s.split("\t")
>
> => ["a", "b"]
>
> 2008/5/30, suresh <suresh.amritap...@gmail.com>:
>
>
>
> > Hi
>
> > I am a newbie to ruby and when i tried to split a string at tab
> > positions, it didnt work. Could you please tell me where I am going
> > wrong? Code is pasted.
>
> > #!/usr/bin/ruby1.9
> > s = "a b"
> > ss = s.split('\t')
> > puts ss[0]
>
> > It just prints the entire line without splitting...
>
> > suresh

Thank you both. It was the double quote issue...when i gave split "\t"
it came correctly....difficulties of a C++ programmer !!!

Stefan Kraemer

5/30/2008 9:37:00 AM

0

Hi,

> ss = s.split('\t')

Regexes take the form /.../ (see http://www.ruby-doc.org/core/classes/R...)

"a b c".split(/\t/) # tabs between the letters!

does what you want.


So long ... Stefan

Mohit Sindhwani

6/2/2008 4:27:00 PM

0

suresh wrote:
> Hi
>
> I am a newbie to ruby and when i tried to split a string at tab
> positions, it didnt work. Could you please tell me where I am going
> wrong? Code is pasted.
>
> #!/usr/bin/ruby1.9
> s = "a b"
> ss = s.split('\t')
> puts ss[0]
>
> It just prints the entire line without splitting...
>
> suresh
>
>
>

The problem is the '\t' - anything inside single quotes is not touched
by Ruby. It needs to be "\t" (double quotes) for it to be interpreted
as a tab character by Ruby.

In my mind, this is the #1 problem that C programmers have in Ruby - it
just seems natural to split at a character \t and C programmers (incl
me) write it as '\t'.

Hope this helps.

Cheers,
Mohit.
6/3/2008 | 12:24 AM.


Mohit Sindhwani

6/2/2008 4:29:00 PM

0

Stefan Kraemer wrote:
> Hi,
>
>> ss = s.split('\t')
>
> Regexes take the form /.../ (see
> http://www.ruby-doc.org/core/classes/R...)
>
> "a b c".split(/\t/) # tabs between the letters!
>
> does what you want.
>
>
> So long ... Stefan
>
>
I think his piece of code also works - in my mind, the problem is the
single quotes surrounding the \t. I've just got done with processing
lots of delimited data and went through the same frustration. ',' would
work but '\t' wouldn't! :)

Cheers,
Mohit.
6/3/2008 | 12:26 AM.