Ask a Question related to Ruby, Design and Development.
-
Thomas Adam #1
Removing "nil" entries from array
Dear All,
I have a really weird situation. I'm reading a file into an array, based
on a grep match. This works fine.....
...yet I re-ran my program using the same data file (unchanged) and now my
array is being intermittently populated with "nil" values. Using "uniq()"
doesn't remove them. Any ideas?
-- Thomas Adam
=====
Thomas Adam
"The Linux Weekend Mechanic" -- [url]www.linuxgazette.com[/url]
__________________________________________________ ______________________
Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger [url]http://mail.messenger.yahoo.co.uk[/url]
Thomas Adam Guest
-
#39298 [Opn->Bgs]: Removing "/" slash works bad
ID: 39298 Updated by: tony2001@php.net Reported By: dankeris at gmail dot com -Status: Open +Status: ... -
Please Wait. Loading...."How to display this on DataGrid while it is getting populated, still not removing all other controls from the web page?"
I would like to display a little pop-up message, or something when the user of Web Page submits data to be populated on DataGrid and not to remove... -
Added text field to database. New entries not equal to "" ???
Hi! I'm accessing an MS Access database from an ASP server. I just added a new text column (field) to one of my tables. I have not added any... -
Mail::Audit (removing attachment fails on "broken path")
On Mon, 25 Aug 2003 12:47:34 GMT "Ian.H " <ian@WINDOZEdigiserv.net> wrote: Yes - but if you want to be sure that you are getting all the worms... -
Removing ".." and "." from a file path
I only found the "File.expand_path" function to make a relative into an absolute path, but what about the ".." and "." components. I need a... -
Joey Gibson #2
Re: Removing "nil" entries from array
On 9/19/2003 9:54 AM, Thomas Adam wrote:
What about using delete_if?>...yet I re-ran my program using the same data file (unchanged) and now my
>array is being intermittently populated with "nil" values. Using "uniq()"
>doesn't remove them. Any ideas?
>
>
a = [:foo, nil, :bar, :baz, nil, nil, "testing"]
a.delete_if {|x| x == nil}
=> [:foo, :bar, :baz, "testing"]
delete_if modifies the array in place. If you want non-destuctive, use
"reject" instead.
--
Dean saor, dean saor an spiorad. Is seinn d'orain beo.
[url]http://www.joeygibson.com[/url]
[url]http://www.joeygibson.com/blog/life/Wisdom.html[/url]
Joey Gibson Guest
-
Michael Garriss #3
Re: Removing "nil" entries from array
Thomas Adam wrote:
[1, nil, 2, nil, 3, nil, 4].compact!>Dear All,
>
>I have a really weird situation. I'm reading a file into an array, based
>on a grep match. This works fine.....
>
>...yet I re-ran my program using the same data file (unchanged) and now my
>array is being intermittently populated with "nil" values. Using "uniq()"
>doesn't remove them. Any ideas?
>
>
>
--> [1, 2, 3, 4]
....or you could use #compact if you don't want to change the array in
place.
Regards,
Michael
Michael Garriss Guest
-
Gavin Sinclair #4
Re: Removing "nil" entries from array
On Friday, September 19, 2003, 11:59:04 PM, Joey wrote:
> On 9/19/2003 9:54 AM, Thomas Adam wrote:>>...yet I re-ran my program using the same data file (unchanged) and now my
>>array is being intermittently populated with "nil" values. Using "uniq()"
>>doesn't remove them. Any ideas?
>>
>>> What about using delete_if?=>> [:foo, :bar, :baz, "testing"]> a = [:foo, nil, :bar, :baz, nil, nil, "testing"]
> a.delete_if {|x| x == nil}
Array#compact is designed for getting rid of nil values.> delete_if modifies the array in place. If you want non-destuctive, use
> "reject" instead.
Gavin
Gavin Sinclair Guest
-
Jason Creighton #5
Re: Removing "nil" entries from array
On Fri, 19 Sep 2003 22:54:24 +0900
Thomas Adam <thomas_adam16@yahoo.com> wrote:
Well, as several people have mentioned, you could use Array#compact, but> Dear All,
>
> I have a really weird situation. I'm reading a file into an array, based
> on a grep match. This works fine.....
>
> ..yet I re-ran my program using the same data file (unchanged) and now my
> array is being intermittently populated with "nil" values. Using "uniq()"
> doesn't remove them. Any ideas?
if I were you, I would try to find out why you're getting nil values.
Could you post the smallest possible code snippet + data that shows this
problem?
Jason Creighton
Jason Creighton Guest



Reply With Quote

