Removing "nil" entries from array

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. #39298 [Opn->Bgs]: Removing "/" slash works bad
      ID: 39298 Updated by: tony2001@php.net Reported By: dankeris at gmail dot com -Status: Open +Status: ...
    2. 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...
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default Re: Removing "nil" entries from array

    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?

    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

  4. #3

    Default Re: Removing "nil" entries from array

    Thomas Adam wrote:
    >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, nil, 2, nil, 3, nil, 4].compact!
    --> [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

  5. #4

    Default 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?
    > 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.
    Array#compact is designed for getting rid of nil values.

    Gavin


    Gavin Sinclair Guest

  6. #5

    Default Re: Removing "nil" entries from array

    On Fri, 19 Sep 2003 22:54:24 +0900
    Thomas Adam <thomas_adam16@yahoo.com> wrote:
    > 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?
    Well, as several people have mentioned, you could use Array#compact, but
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139