Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > assign the array returned by String.split to a variable

Reply
Thread Tools

assign the array returned by String.split to a variable

 
 
Catsquotl
Guest
Posts: n/a
 
      06-03-2009
Hi

i have the following method
-----
def create
length = @txt.length
i = 0
while i < (length + 1)
line = @txt[i]
line.split(',')
#@arr.push(a)
i += 1
end
end
-------
this works but as soon as i take out the comment.
i get an error something about private method split beeing called on a
nil object.
I found out that this has to do with split that has to be called by self
or something.

what i like to do is have an array of the split arrays.
any ideas?

greet Eelco
 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      06-03-2009
Catsquotl wrote:
> this works


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

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      06-03-2009
> assign the array returned by String.split to a variable

str = "hello,world"
arr = str.split(",")
p arr

--output:--
["hello", "world"]
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
John W Higgins
Guest
Posts: n/a
 
      06-03-2009
[Note: parts of this message were removed to make it a legal post.]

On Wed, Jun 3, 2009 at 9:35 AM, Catsquotl <(E-Mail Removed)> wrote:

> Hi
>
>
>
> what i like to do is have an array of the split arrays.
> any ideas?
>



You would want something like this

def create
ary = @txt.collect{ |t| t.split(',') }
end

Basically collect takes your initial array and runs through each element and
creates a new array based on the results of the block.

John

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      06-03-2009
Catsquotl wrote:
> Hi
>
> i have the following method
> -----
> def create
> length = @txt.length
> i = 0
> while i < (length + 1)
> line = @txt[i]
> line.split(',')
> #@arr.push(a)
> i += 1
> end
> end
> -------
> this works but as soon as i take out the comment.
> i get an error something about private method split beeing called on a
> nil object.


This means that @arr is nil, that is, you are doing

nil.push(a)

So you need to initialize it first:

@arr = []

There are a few other errors in your code, for example you didn't assign
to a:

a = line.split(',')
@arr.push(a)

and your loop should be while i < length, not while i < length+1. As has
been pointed out, there are more ruby-like ways to do this loop.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
assign JavaScript variable to Java variable problem in JSP Matt Java 8 01-25-2010 04:48 PM
How to Assign JavaScript variable to Jsp Variable Ranjan ASP .Net 1 04-10-2007 03:35 PM
Assign reference to hash returned from a function Hobo Salesman Perl Misc 7 06-17-2006 05:01 AM
assign a struct variable into another struct variable ineedyourluvin1@yahoo.com C++ 18 07-22-2005 04:08 PM
Assign a name to a pointer variable from another variable Nmishra C Programming 3 05-24-2004 03:57 PM



Advertisments