[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

File Output Problem

aidy

5/19/2006 12:22:00 PM

Hi,

I am reading a txt file, e.g.

;Country Depot EmployeeID FirstName LastName UserID
null BERLIN G744ahe null Rutter 123

This is the code:

def enter_employees()
f = File.new('c:\employee_data.txt')
f.each do |line|
if !line.include? ";"
data = line.chomp.split(' ')
i = 0
while i < data.length do
if data[i] != "null" or data[i] != nil
puts "i = #{i}"
puts data [i]
i += 1
end
end
end
end
end
enter_employees()

I am trying not to print to the console if data[i] == "null", however
this is the output

i = 0
null
i = 1
BERLIN
i = 2
G744ah
i = 3
null
i = 4
Rutter
i = 5
123

Could someone tell me what I am doing wrong?

Thanks

Aidy

7 Answers

Chris Hulan

5/19/2006 12:45:00 PM

0

This line:
if data[i] != "null" or data[i] != nil
should be
if data[i] != "null" and data[i] != nil

In the first case when data[i]=="null" than data[i] != nill so the
statement is True, and likewise if data[i]==nil than data[i] != "null"
and the statement is again True

lloyd@binaryten.com

5/19/2006 12:57:00 PM

0

you could also use:

unless data[i] =="null" or data[i] == nil
puts "i = #{i}"
puts data[i]

some people make better sense using unless because the logic isnt
negated and reads more logically. personally i have no preference but
it may work for you

Robo

5/19/2006 1:18:00 PM

0

The if statement should be using "and" instead of "or", then put i += 1
outside the if statement.

Or, use the more ruby way: data.each_index do |i|

Robo

aidy wrote:
> Hi,
>
> I am reading a txt file, e.g.
>
> ;Country Depot EmployeeID FirstName LastName UserID
> null BERLIN G744ahe null Rutter 123
>
> This is the code:
>
> def enter_employees()
> f = File.new('c:\employee_data.txt')
> f.each do |line|
> if !line.include? ";"
> data = line.chomp.split(' ')
> i = 0
> while i < data.length do
> if data[i] != "null" or data[i] != nil
> puts "i = #{i}"
> puts data [i]
> i += 1
> end
> end
> end
> end
> end
> enter_employees()
>
> I am trying not to print to the console if data[i] == "null", however
> this is the output
>
> i = 0
> null
> i = 1
> BERLIN
> i = 2
> G744ah
> i = 3
> null
> i = 4
> Rutter
> i = 5
> 123
>
> Could someone tell me what I am doing wrong?
>
> Thanks
>
> Aidy
>

aidy

5/19/2006 1:51:00 PM

0

Not so sure whether you are supposed to say thanks on the list; but
thanks for the help and ideas.

Aidy

Chris Hulan

5/19/2006 2:04:00 PM

0

Here's how I'd write it:

def enter_employees()
File.open('employee_data.txt','r'){|f| #use block so file is
automatically closed
f.each_line { |line|
next if /^;/ =~ line #check for ';' at start of line
i = 0
line.chomp.split(' ').each{|x|
next if x.nil? or x == "null" #skip nil or "null" values
puts "i = #{i}", x
i += 1
}
}
}
end

Cheers
Chris

Robert Klemme

5/19/2006 3:23:00 PM

0

aidy wrote:
> Hi,
>
> I am reading a txt file, e.g.
>
> ;Country Depot EmployeeID FirstName LastName UserID
> null BERLIN G744ahe null Rutter 123
>
> This is the code:
>
> def enter_employees()
> f = File.new('c:\employee_data.txt')
> f.each do |line|
> if !line.include? ";"
> data = line.chomp.split(' ')
> i = 0
> while i < data.length do
> if data[i] != "null" or data[i] != nil
> puts "i = #{i}"
> puts data [i]
> i += 1
> end
> end
> end
> end
> end
> enter_employees()
>
> I am trying not to print to the console if data[i] == "null", however
> this is the output
>
> i = 0
> null
> i = 1
> BERLIN
> i = 2
> G744ah
> i = 3
> null
> i = 4
> Rutter
> i = 5
> 123
>
> Could someone tell me what I am doing wrong?
>
> Thanks
>
> Aidy

Dunno whether this is what you want...

17:21:54 [Temp]: cat dat
;Country Depot EmployeeID FirstName LastName UserID
null BERLIN G744ahe null Rutter 123
17:22:00 [Temp]: ruby -naF\\s+ -e '/^;/ =~ $_ or puts $F.reject {|x|
"null"==x}' dat
BERLIN
G744ahe
Rutter
123

Kind regards

robert

Mike Painter

10/9/2008 1:56:00 AM

0

CB wrote:
> On Tue, 7 Oct 2008 19:00:16 -0700, "Mike Painter"
> <mddotpainter@sbcglobal.net> wrote:
>
>> CB wrote:
>>> On Mon, 6 Oct 2008 19:55:34 -0700, "Mike Painter"
>>> <mddotpainter@sbcglobal.net> wrote:
>>>
>>>
>>>
>>> Please do some study on what Paul said about the law.
>>> Then get back to me.
>>
>> You continue to make arbitrary choices based on what you want.
>> Clearly your bible is contradictory at any level.
>> You claim Paul is the authority
>>
>> Some claim the words of Jesus are the authority
>>
>> Matthew 5:18-19
>> Till heaven and earth pass, one jot or tittle shall nowise pass from
>> the law, till all be fulfilled. Whosoever therefore shall break one
>> of these least commandments, and shall teach men so, he shall be
>> called the least in the kingdom of heaven.
>>
>> Luke 16:17
>> It is easier for heaven and earth to pass, than one tittle of the
>> law to fail.
>
> Jesus said that while still under the law.
> Nice try.

Not accorsing to Luke.
But even if this was true either what he said was true or it was not.
If it was not true then he lied.


>>
>> And I'm sure that some will claim Luke as the authority
>> Luke 16:16
>> The law and the prophets were until John [the Baptist]: since that
>> time the kingdom of heaven is preached.
>
> And it was preached. The Kingdom of God was not established until the
> day of Pentecost.
>
> Like I said, get back to me when you get an education.
> Try 2nd grade.


It's "As I said" and Luke contradicts what you claim. Only one can be right
and all can be wrong.

You are just another salad bar christian who claims to use the whole bible
but picks and chooses what you want.