split question

L

Li Chen

Hi all,

I try to split a line that is separated by 'tab' and expect return an
array with all the elements containing empty space. But my code doesn't
work. Any idea?

Thanks,

Li

C:\Documents and Settings\chen41\Desktop>irb
irb(main):001:0> s='\t\t\t\t\t\t\t\t'
=> "\\t\\t\\t\\t\\t\\t\\t\\t"
irb(main):002:0> s.split(/\t/)
=> ["\\t\\t\\t\\t\\t\\t\\t\\t"]
irb(main):003:0> s.split(/\\t/)
=> []
irb(main):004:0>
 
R

Ryan Davis

Hi all,

I try to split a line that is separated by 'tab' and expect return an
array with all the elements containing empty space. But my code
doesn't
work. Any idea?

Thanks,

Li

C:\Documents and Settings\chen41\Desktop>irb
irb(main):001:0> s='\t\t\t\t\t\t\t\t'
=> "\\t\\t\\t\\t\\t\\t\\t\\t"

These aren't tabs. They're "\" followed by "t", you can tell because
the "\" is backslashed, meaning it is a literal backslash.

Use double quotes instead.
irb(main):002:0> s.split(/\t/)
=> ["\\t\\t\\t\\t\\t\\t\\t\\t"]
irb(main):003:0> s.split(/\\t/)
=> []
irb(main):004:0>
 
J

Josh Cheek

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

Hi all,

I try to split a line that is separated by 'tab' and expect return an
array with all the elements containing empty space. But my code doesn't
work. Any idea?

Thanks,

Li

C:\Documents and Settings\chen41\Desktop>irb
irb(main):001:0> s='\t\t\t\t\t\t\t\t'
=> "\\t\\t\\t\\t\\t\\t\\t\\t"
irb(main):002:0> s.split(/\t/)
=> ["\\t\\t\\t\\t\\t\\t\\t\\t"]
irb(main):003:0> s.split(/\\t/)
=> []
irb(main):004:0>
Single quotes treat things literally, like you can't interpolate, and \t is
treated as two separate characters.

$irb
=> "1.8.6"
=> "\\t\\t\\t\\t\\t\\t\\t\\t"
=> "\t\t\t\t\t\t\t\t"
=> []
=> "abc\#{2}def"
=> "abc2def"
 
L

Li Chen

Ryan said:
These aren't tabs. They're "\" followed by "t", you can tell because
the "\" is backslashed, meaning it is a literal backslash.

Use double quotes instead.

Here is what I get using double quotes. I get an empty array only.


C:\Documents and Settings\chen41\Desktop>irb
irb(main):001:0> s="\t\t\t\t\t\t\t\t"
=> "\t\t\t\t\t\t\t\t"
irb(main):002:0> s.split(/\t/)
=> []
irb(main):003:0>
 
S

Siep Korteling

Li said:
Ryan said:
These aren't tabs. They're "\" followed by "t", you can tell because
the "\" is backslashed, meaning it is a literal backslash.

Use double quotes instead.

Here is what I get using double quotes. I get an empty array only.


C:\Documents and Settings\chen41\Desktop>irb
irb(main):001:0> s="\t\t\t\t\t\t\t\t"
=> "\t\t\t\t\t\t\t\t"
irb(main):002:0> s.split(/\t/)
=> []
irb(main):003:0>

str.split takes two parameters: the pattern and optionally the "limit"
From the docs:
If the limit parameter is omitted, trailing null fields are suppressed.
If limit is a positive number, at most that number of fields will be
returned (if limit is 1, the entire string is returned as the only entry
in an array). If negative, there is no limit to the number of fields
returned, and trailing null fields are not suppressed.

So

s = "\t\t\t\t"
s.split("\t") #all null fields
#=> []
s.split("\t",-1)
#=> ["", "", "", ""]

hth,

Siep
 
D

David A. Black

Hi --

Li said:
Ryan said:
On Oct 7, 2009, at 14:29 , Li Chen wrote:


C:\Documents and Settings\chen41\Desktop>irb
irb(main):001:0> s='\t\t\t\t\t\t\t\t'
=> "\\t\\t\\t\\t\\t\\t\\t\\t"

These aren't tabs. They're "\" followed by "t", you can tell because
the "\" is backslashed, meaning it is a literal backslash.

Use double quotes instead.

Here is what I get using double quotes. I get an empty array only.


C:\Documents and Settings\chen41\Desktop>irb
irb(main):001:0> s="\t\t\t\t\t\t\t\t"
=> "\t\t\t\t\t\t\t\t"
irb(main):002:0> s.split(/\t/)
=> []
irb(main):003:0>

str.split takes two parameters: the pattern and optionally the "limit"
From the docs:
If the limit parameter is omitted, trailing null fields are suppressed.
If limit is a positive number, at most that number of fields will be
returned (if limit is 1, the entire string is returned as the only entry
in an array). If negative, there is no limit to the number of fields
returned, and trailing null fields are not suppressed.

So

s = "\t\t\t\t"
s.split("\t") #all null fields
#=> []
s.split("\t",-1)
#=> ["", "", "", ""]

["", "", "", "", ""] I think :)


David

--
The Ruby training with D. Black, G. Brown, J.McAnally
Compleat Jan 22-23, 2010, Tampa, FL
Rubyist http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top