[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Iterating over a nil value from CSV element

Max Russell

3/6/2008 5:25:00 PM

when I'm parsing a csv file, and don't want to use those elements that
return a nil value, what is the easiest way to do so? I've tried:

if record == nil
next
else

where record is the current element from the file.
--
Posted via http://www.ruby-....

2 Answers

Lars Christensen

3/7/2008 10:06:00 AM

0

On Mar 6, 6:24 pm, Max Russell <thedoss...@gmail.com> wrote:
> when I'm parsing a csv file, and don't want to use those elements that
> return a nil value, what is the easiest way to do so? I've tried:
>
> if record == nil
> next
> else

If tend to like boolean expressions rather than comparison with
'nil' (or false):

next if record.nil?

or

next unless record

The latter will skip when next is either 'nil' or 'false'.

If you have multiple fields and want to skip if they all are nil/
false:

next unless first or second or third

If you want to skip if any of them are nil/false:

next unless first and second and third

Lars

Todd Benson

3/7/2008 4:57:00 PM

0

On Thu, Mar 6, 2008 at 11:24 AM, Max Russell <thedossone@gmail.com> wrote:
> when I'm parsing a csv file, and don't want to use those elements that
> return a nil value, what is the easiest way to do so? I've tried:
>
> if record == nil
> next
> else
>
> where record is the current element from the file.

Depending on what you are doing, you can parse first, use #compact after.

Todd